title: Unhandled NullReference exception when closing WPF application
body: <p>I'm getting an unhandled exception in my application when I close the last window:</p>
  
  <blockquote>
    <p>An unhandled exception of type 'System.NullReferenceException'
    occurred in PresentationFramework.dll</p>
    
    <p>Additional information: Object reference not set to an instance of an
    object.</p>
  </blockquote>
  
  <p>This only occurs if, during the application's lifetime, I open a child window through a certain process that I have set up. The window exists in another assembly that is loaded at runtime dynamically with MEF, and then instantiated with Castle. If I then call a certain method, it creates a new STA thread and opens a WPF dialog window.</p>
  
  <p>Some caveats:</p>
  
  <ul>
  <li>This only happens on certain machines/environments (I'm not able to discern a pattern though)</li>
  <li>I have an UnhandledException handler on the dispatcher for the application which catches all unhandled exceptions. This is not caught by that.</li>
  </ul>
  
  <p>The call stack is:</p>
  
  <pre><code>PresentationFramework.dll!MS.Internal.Controls.ConnectionPointCookie.Disconnect()
  PresentationFramework.dll!MS.Internal.Controls.ConnectionPointCookie.Finalize()
  </code></pre>
  
  <p>Has anyone seen this before, or would anyone know how to debug this? It's strange that there's no call stack and it happens right as the program is exiting.</p>