title: How to release a SAFEARRAY within another SAFEARRAY
body: <p>I am using a .NET component from MFC/C++ through a COM interface (using .NET Interop). The interface contains structures containing arrays (resulting in SAFEARRAYS in COM).</p>
  
  <p>I am having trouble releasing the SAFEARRAYs in the correct way. When running the application, the application will crash at the second SafeArrayDestroy. So, will the inner SAFEARRAY be deleted, when deleting the outer SAFAARRAY?
  Any other thoughts?</p>
  
  <p>Thanks!</p>
  
  <p>The C# interface looks like this:</p>
  
  <pre><code>public struct structVersionCondition
  {
      public Int32 m_val;
  }
  public struct structVersion
  {
      public structVersionCondition[] m_VersionConditions;
  }
  public struct structVersions
  {
      public structVersion[] m_Version;
  }
  
  public interface TextStore_Interface
  {
      [DispId(1)]
      bool GetVersionRef(out structVersions versions);
  }
  </code></pre>
  
  <p>The resulting COM IF is called from MFC/C++ and looks like this:</p>
  
  <pre><code>m_pIF-&gt;GetVersionRef(&amp;ver, &amp;retVal);
  SafeArrayAccessData(ver.m_Version, (void**)&amp;pValsOuter);
  //..Get Lower and Upper bound calculate number of elements
  for (int i=0; i&lt;elem_no; ++i){
      SafeArrayAccessData(pValsOuter[i].m_VersionConditions, (void**)&amp;pValsInner);
      //operate on the structVersionCondition[] SAFEARRAY trough pValsInner
      SafeArrayUnaccessData(pValsOuter[i].m_VersionConditions);
      SafeArrayDestroy(pValsOuter[i].m_VersionConditions);
  }
  SafeArrayUnaccessData(ver.m_Version);
  SafeArrayDestroy(ver.m_Version); //Exception is thrown here!
  </code></pre>
  
  <p>Regards
  John</p>