title: Why won't Direct3D recover after unplugging a monitor in Windows XP?
body: <p>An interesting bug came up that I'm having no luck with.  In a windowed Direct3D9 program using native code, I handle a device lost using something similar to the following:</p>
  
  <pre><code>void MyClass::RecoverFromDeviceLost(LPDIRECT3DDEVICE9 deviceToRecover, D3DPRESENT_PARAMETERS devicePresentParams )
  {
  
  
      HRESULT hr = deviceToRecover-&gt;TestCooperativeLevel();
      if(hr == D3DERR_DEVICELOST ) { 
          //Code to shutdown all D3DPOOL_DEFAULT allocated objects
  
      }else if(hr == D3DERR_DEVICENOTRESET){
  
          hr = deviceToRecover-&gt;Reset(&amp;devicePresentParams);
          if(SUCCEEDED(hr))
          {
              //Code to rebuild all D3DPOOL_DEFAULT objects
  
          }
  
      }
  
  }
  </code></pre>
  
  <p>This works fine on Vista, but seems to have major problems on XP.  If the monitor is unplugged, or switched away from the PC via a KVM, I never receive the <code>D3DERR_DEVICELOST</code>.  The only return value from TestCooperativeLevel I ever receive is <code>D3DERR_DEVICENOTRESET</code>.  And every call to Reset gives a D3DERR_INVALIDCALL.  I tried forcing the program to use the shutdown code by doing this:</p>
  
  <pre><code>...
  else if(hr == D3DERR_DEVICENOTRESET){
  
          hr = deviceToRecover-&gt;Reset(&amp;devicePresentParams);
          if(SUCCEEDED(hr))
          {
              //Code to rebuild all D3DPOOL_DEFAULT objects
  
          }else {
              //Duplicate of code to shutdown all D3DPOOL_DEFAULT objects
          }
  
      }
  ...
  </code></pre>
  
  <p>But there was no change.  This problem only seems to affect Windows XP (so far tested on SP2, SP3).  I am using the August 2007 DXSDK, and can't update at this time. Has anyone seen this problem before, or have any idea why I can't reset my device?</p>
  
  <p><strong>UPDATE:</strong> I believe I have <a href="https://stackoverflow.com/questions/1462078/why-wont-direct3d-recover-after-unplugging-a-monitor-in-windows-xp/1474071#1474071">found a solution</a>, but am still perplexed by the failure of the second code segment listed above.  After getting the DirectX Debug runtime to work over remote debugging, I realized the reason that the Reset function kept failing was because there were unreleased resources.  However, the exact same release code, when applied as shown in the answer, resolved the issue.  I did verify that the program was not creating D3DPOOL_DEFAULT objects between calls to the recover function. Is there something in the structure of Direct3D that could cause a problem if performing a reset as shown in the this question's code segments?  </p>