title: Failed to release Direct3D Device "or" Corresponding Context
body: <p>Yes, its actually <strong>"or"</strong>. I shall explain. I am developing helper classes for myself, like DirectXToolKit. For managing COMs I`m using <strong>Microsoft::WRL::ComPtr&lt;<em>T</em>></strong> <em>(wrl.h)</em>.  </p>
  
  <pre class="lang-cpp prettyprint-override"><code>struct Renderer
  {
      ComPtr&lt;ID3D11Device&gt; m_Device;
      ComPtr&lt;ID3D11DeviceContext&gt; m_ImmContext;
  }
  </code></pre>
  
  <p>When all the resources are destroyed, the instance of the structure above should be destroyed as well, but after the dtor is called, I am triggering the error in <strong>Microsoft::WRL::ComPtr&lt;<em>T</em>></strong>, when it tries to release the device or context. </p>
  
  <p>I have implemented the dtor where I manually release m_Device and m_ImmContext, but unfortunately the last member I try to release always encounters an issue in the function</p>
  
  <pre class="lang-cpp prettyprint-override"><code>unsigned long InternalRelease() throw()
  {
      unsigned long ref = 0;
      T* temp = ptr_;
  
      if (temp != nullptr)
      {
          ptr_ = nullptr;
          ref = temp-&gt;Release();
      }
  
      return ref;
  }
  </code></pre>
  
  <p>here</p>
  
  <pre><code>ref = temp-&gt;Release();
  </code></pre>
  
  <p>When I succeed releasing the device first, the context triggers the error, and vice versa (! yes, when one of them was successfully released, the destuction of the second member  fails). There was already a question like mine (<a href="https://stackoverflow.com/questions/12538287/destroy-directx-device-and-swap-chain">destroy directx device and swap chain</a>), but the window and swapchain are already destroyed, like the other dx resources. Have no clue why is this happening. Any ideas?</p>
  
  <p>Sorry for my imperfect English :3</p>