How to release the directX device after a crash

I know that we have to use the cudaD3D9End() function to cleanup the directX initialization.
But if the program crashes, it never reaches that call and it seems the directX device is never released. Even if you restart your application or any other CUDA directX application (one of the examples) you get the same error -
“unspecified error at line xyz” the line number is generally where you make the cudaD3D0Begin() call.

The only solution I have found till now is (you guessed it) RESTARTING the machine. :thumbsdown:

Any help/comments/suggestions appreciated. Thanks.