title: Why do I leak memory when using CComSafeArray<BSTR>?
body: <p>I have a memory leak which I've narrowed down to the following code.  What's going on?</p>
  
  <pre><code>CComSafeArray&lt;BSTR&gt; sa(5);
  sa[0] = SysAllocString("constant");
  </code></pre>
  
  <p><a href="https://msdn.microsoft.com/en-us/library/8f97ayy5.aspx" rel="nofollow"><code>CComSafeArray::~CComSafeArray</code></a> calls <code>SafeArrayDestroy</code> (via <code>CComSafeArray::Destroy()</code>, see below), which <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms221702(v=VS.85).aspx" rel="nofollow">should then call <code>SysFreeString</code></a> on all members.  As such there shouldn't be a leak here.  What's going on?</p>
  
  <pre><code>HRESULT Destroy()
  {
      HRESULT hRes = S_OK;
      if (m_psa != NULL)
      {
          hRes = Unlock();
          if (SUCCEEDED(hRes))
          {
              hRes = SafeArrayDestroy(m_psa);
              if (SUCCEEDED(hRes))
                  m_psa = NULL;
          }
      }
      return hRes;
  }
  </code></pre>