title: How to avoid race on RCW cleanup
body: <p>I have a gui application, that periodically shows the cpu load. The load is read by a StateReader class:</p>
  
  <pre><code>public class StateReader
  {
      ManagementObjectSearcher searcher;
  
      public StateReader()
      {
          ManagementScope scope = new ManagementScope("\\\\localhost\\root\\cimv2");
          ObjectQuery query = new ObjectQuery("select Name,PercentProcessorTime from Win32_PerfFormattedData_PerfOS_Processor where not Name='_Total'");
          searcher = new ManagementObjectSearcher(scope, query);
      }
  
      // give the maximum load over all cores
      public UInt64 CPULoad()
      {
          List&lt;UInt64&gt; list = new List&lt;UInt64&gt;();
          ManagementObjectCollection results = searcher.Get();
          foreach (ManagementObject result in results)
          {
              list.Add((UInt64)result.Properties["PercentProcessorTime"].Value); 
          }
          return list.Max();
      }
  }
  </code></pre>
  
  <p>The gui is updated using reactive extensions:</p>
  
  <pre><code>var gui = new GUI();
  var reader = new StateReader();
  
  var sub = Observable.Interval(TimeSpan.FromSeconds(0.5))
                      .Select(_ =&gt; reader.CPULoad())
                      .ObserveOn(gui)
                      .Subscribe(gui.ShowCPUState);
  
  Application.Run(gui);
  sub.Dispose();
  </code></pre>
  
  <p>Now when I exit my application, I get an error saying </p>
  
  <pre><code>RaceOnRCWCleanup was detected. 
  An attempt has been mad to free an RCW that is in use. The RCW is use on the 
  active thread or another thread. Attempting to free an in-use RCW can cause 
  corruption or data loss.
  </code></pre>
  
  <p>This error doesn't appear if I don't read the cpu load, but just supply some random value, so the error is somehow connected to reading the load. Also if I put a breakpoint after <code>Application.Run(gui)</code> and wait there for a bit, the error doesn't seem to come as often. </p>
  
  <p>From this and from my googling I think that using the classes in the management namespace creates a background thread that references a COM object wrapped in a Runtime Callable Wrapper, and when I exit my application, that thread doesn't have time to properly close the RCW, leading to my error. Is this correct, and how can I solve this problem?</p>
  
  <hr>
  
  <p>I have edited my code to reflect the responses I have got, but I still get the same error. The code is updated on three points:</p>
  
  <ul>
  <li>StateReader is Disposable, disposes its ManagementObjectSearcher in the Dispose method
  and I call Dispose on the StateReader object after Application.Run in my main method</li>
  <li>In CPULoad I dispose of the ManagementCollection and each ManagementObject in it</li>
  <li>In my main method I dispose of the subscription object in an event handler on FormClosing<br>
  on the gui. This should ensure that no events are generated for the gui after it is closed.</li>
  </ul>
  
  <p>The relevant parts of the code are now, in StateReader:</p>
  
  <pre><code>// give the maximum load over all cores
  public UInt64 CPULoad()
  {
      List&lt;UInt64&gt; list = new List&lt;UInt64&gt;();
      using (ManagementObjectCollection results = searcher.Get())
      {
          foreach (ManagementObject result in results)
          {
              list.Add((UInt64)result.Properties["PercentProcessorTime"].Value); 
              result.Dispose();
          }
      }
      return list.Max();
  }
  
  public void Dispose()
  {
      searcher.Dispose();
  }
  </code></pre>
  
  <p>And in my main:</p>
  
  <pre><code>gui.FormClosing += (a1, a2) =&gt; sub.Dispose();
  
  Application.Run(gui);
  reader.Dispose();
  </code></pre>
  
  <p>Is there anything else I could do to avoid the error I get?</p>