title: IShellDisptach: Why does FolderItemVerbs::Release() + CoUninitialize() crash?
body: <p>I'm having a very bizarre problem with the <strong>IShellDispatch</strong> COM interface, more specifically with the <strong>FolderItemVerbs</strong> object, that drives me nuts!</p>
  
  <p>Calling <em>FolderItemVerbs::Release()</em> followed by <em>CoUninitialze()</em> will result in crash. It's clearly reproducible, but only happens 1 out of 10 times.</p>
  
  <p>The crash is an "0xC0000005: Access Violation" error. Running the problematic code in a loop 100% reproduces the crash sooner or later :-(</p>
  
  <p>Please see the example program:</p>
  
  <pre><code>static int TestProc(const TCHAR *pcDirectoryName, const TCHAR *pcFileName)
  {
      int iSuccess = 0;
  
      IShellDispatch *pShellDispatch = NULL;
      Folder *pFolder = NULL; FolderItem *pItem = NULL;
      FolderItemVerbs *pVerbs = NULL;
  
      HRESULT hr = CoCreateInstance(CLSID_Shell, NULL, CLSCTX_INPROC_SERVER, IID_IShellDispatch, (void**)&amp;pShellDispatch);
      if(FAILED(hr) || (pShellDispatch ==  NULL))
      {
          iSuccess = -3;
          return iSuccess;
      }
  
      variant_t vaDirectory(pcDirectoryName);
      hr = pShellDispatch-&gt;NameSpace(vaDirectory, &amp;pFolder);
      if(FAILED(hr) || (pFolder == NULL))
      {
          iSuccess = -4;
          pShellDispatch-&gt;Release();
          return iSuccess;
      }
  
      variant_t vaFileName(pcFileName);
      hr = pFolder-&gt;ParseName(vaFileName, &amp;pItem);
      if(FAILED(hr) || (pItem == NULL))
      {
          iSuccess = -5;
          pFolder-&gt;Release();
          pShellDispatch-&gt;Release();
          return iSuccess;
      }
  
      hr = pItem-&gt;Verbs(&amp;pVerbs);
      if(FAILED(hr) || (pVerbs == NULL))
      {
          iSuccess = -6;
          pItem-&gt;Release();
          pFolder-&gt;Release();
          pShellDispatch-&gt;Release();
          return iSuccess;
      }
  
      /* Here we would do something with the FolderItemVerbs */
  
      pVerbs-&gt;Release(); pVerbs = NULL; //If this line is commented out, we don't get a crash, but a massive memory leak!
      pItem-&gt;Release(); pItem = NULL;
      pFolder-&gt;Release(); pFolder = NULL;
      pShellDispatch-&gt;Release(); pShellDispatch = NULL;
  
      iSuccess = 1;
      return iSuccess;
  }
  
  //-----------------------------------------------------------------------------
  
  static unsigned __stdcall ThreadProc(void* pArguments)
  {
      HRESULT hr = CoInitialize(NULL);
      if((hr == S_OK) || (hr == S_FALSE))
      {
          threadParam_t *params = (threadParam_t*) pArguments;
          params-&gt;returnValue = TestProc(params-&gt;pcDirectoryName, params-&gt;pcFileName);
          CoUninitialize();
      }
      else
      {
          if(threadParam_t *params = (threadParam_t*) pArguments)
          {
              params-&gt;returnValue = -10;
          }
      }
  
      return EXIT_SUCCESS;
  }  
  </code></pre>
  
  <p>Please download the <em>complete</em> example code is here:
  <a href="http://pastie.org/private/0xsnajpia9lsmgnlf2afa" rel="nofollow">http://pastie.org/private/0xsnajpia9lsmgnlf2afa</a></p>
  
  <p>Please also note that I unambiguously tracked down to crash to <strong>FolderItemVerbs</strong>, because if I never create the <em>FolderItemVerbs</em> object, the crash is gone immediately.</p>
  
  <p>Also if I never call "pVerbs->Release()" before <em>CoUninitialize()</em> the crash is gone too, but this will result in a massive memleak, obviously.</p>
  
  <p>Another strange thing is that the crash will NOT happen, if I run the program under the Debugger! But I can run the program, wait for the crash and then let the Debugger handle the crash.</p>
  
  <p>Unfortunately the Stack Trace that I get then doesn't help much:
  <a href="http://pastie.org/private/cuwunlun2t5dc5lembpw" rel="nofollow">http://pastie.org/private/cuwunlun2t5dc5lembpw</a></p>
  
  <p>I don't think I'm doing anything wrong here. I have checked the code over and over again in the last two days. So this all seems to be a bug in FolderItemVerbs!</p>
  
  <p>Has anybody encountered this before and/or can confirm that this is a bug in FolderItemVerbs? Also, is there any workaround for the problem?</p>
  
  <p>Thanks in advance !!! </p>