title: What is the correct way to pass data from MTA to STA in COM and vise versa
body: <p>I have a multhithreaded application which works sitting in MTA. It performs calls of outproc object (let's name it OUTPROC_OBJ_A) and everything works fine:
  each thread calls CoInitializeEx(NULL, COINIT_MULTITHREADED).</p>
  
  <p>But now, I have yet another couple of com objects sitting in a dll - I need to use it in my application. Lets name one of it INPROC_OBJ_B. Their threading model is apartment threading. Also, it shares interfaces from OUTPROC_OBJ_A (it uses some of data as arguments of methods importing it from .tlb file)</p>
  
  <p>When I do something like this (in one thread):</p>
  
  <pre><code>idl 1:
  interface IData : IDispatch
  {
  }
  
  coclass ConcreteData : IData;
  
  idl 2: (uses idl 1):
  interface IOBJ_B : IDispatch
  {
      void initData([in, out] IData* data);
  }
  
  coclass INPROC_OBJ_B : IOBJ_B;
  
  main:
  {
  
      CoInitializeEx(NULL, COINIT_MULTITHREADED);
      ...
  
      {
          // 1.) Create instance of INPROC_OBJ_B.
          CComPtr&lt;IOBJ_B&gt; pOBJ_B = CreateInstance(INPROC_OBJ_B);
          // 2.) Pass to some method of INPROC_OBJ_B some data (For example, pointer to another com object created here.)
          CComPtr&lt;IData&gt; pData = CreateInstance(ConcreteData);
  
          pOBJ_B-&gt;initData(pData);
  
          // 3.) Here it crashes. Probably on release of pData. 
          // initData now is empty and does nothing.
      }
  
      CoUninitialize();
  }
  </code></pre>
  
  <p>When I do this, it can work fine sometimes. But sometimes, it crashes the entire application with access violation. Sometimes it throws object_is_not_connected_exception.</p>
  
  <p>When I replace COINIT_MULTITHREADED with COINIT_APARTMENTTHREADED in CoInitializeEx, it works fine. But I can't do this in my application - The entire infrastructure is in MTA.</p>
  
  <p>[UPDATE] : more pseudo code in example.</p>
  
  <p>What am I doing wrong?</p>