title: RaceOnRCWCleanup when closing WPF application with COM and "Enable unmanaged code debugging"
body: <p>During development of my WPF application I suddenly got the seemingly much-dreaded RaceOnRCWCleanup when closing the application while debugging. When executing without debugger attached everything was normal.</p>
  
  <p>Image and text of the warning message:</p>
  
  <p><img src="https://i.stack.imgur.com/7UD4E.png" alt="enter image description here"></p>
  
  <blockquote>
    <p>Managed Debugging Assistant
    'RaceOnRCWCleanup' has detected a
    problem in
    'MyEXE'. Additional Information:
    An attempt has been made to free an
    RCW that is in use.  The RCW is in use
    on the active thread or another
    thread.  Attempting to free an in-use
    RCW can cause corruption or data loss.</p>
  </blockquote>
  
  <p>The stack trace was not very helpful:</p>
  
  <pre>   [External Code] 
  >   mscoreei.dll!000007fef75c3309()     
      [Frames below may be incorrect and/or missing, no symbols loaded for mscoreei.dll]  
      mscoree.dll!000007fef7655b21()  
      kernel32.dll!0000000077b7f56d()     
      ntdll.dll!0000000077cb2cc1()    </pre>
  
  <p>Since the last bigger change I had made prior to the error occuring for the first time was using a COM object from my managed C# code I obviously suspected that OCX (written by me). Searching the net and stackoverflow did not yield any solution.</p>
  
  <p>Then I found out that <strong>this only happens when the setting "Enable unmanaged code debugging" is activated</strong>. I had done that, because I wanted to debug my OCX written in unmanaged C++, too. Disabling unmanaged code debugging made the problem go away immediately.</p>
  
  <p>This happened on Windows 7, Visual Studio 2010 without SP1.</p>
  
  <p>Does anyone have an explanation what is happening here? Is this a bug?</p>
  
  <p><strong>Update:</strong> This problem was gone when I installed service pack 1 for Visual Studio 2010.</p>