Crash when releasing device when app is closed

Started by
14 comments, last by Dave Hunt 18 years, 1 month ago
I get a crash when executing this:
if(g_pd3dDevice!=NULL) {
	g_pd3dDevice->Release();
}
Shouldn't this piece of code be absolutely guaranteed to be crash free? I'm sure it's this part where the crash comes, but not sure if this is the code causing it. I've put in test write out to an error log file before and after this piece of code and cornered the crash to happening exactly when Release() is called. Please tell me which further info is needed to diagnose this problem because I have no idea!
Advertisement
If you are using visual studio why not make use of its rather excellent built in debugger. You can change the directx runtime to debug and put the debug output level to maximum (this is in your control panel). Then if something nasty happens, visual studio's output window gets filled up with helpful information [smile]

Regards,
ViLiO
Richard 'ViLiO' Thomasv.net | Twitter | YouTube
I added a line below to your code.

if(g_pd3dDevice!=NULL) {	g_pd3dDevice->Release();        g_pd3dDevice=NULL;}


The idea is that g_pd3dDevice will always be null if its not initialized.

Therefore, when you start your program (or class in the constuctor), you need to set the g_pd3dDevice = NULL before its ever used or looked at. Also, when you release it, you need to set the variable back to NULL. Just because its ->Release()'d, doesn't mean the g_pd3dDevice = NULL. So you need to set it back.
Quote:Original post by all_names_taken
Shouldn't this piece of code be absolutely guaranteed to be crash free?


No. If you have another call to g_pd3dDevice->Release() somewhere else in your code (or the one that's crashing is called more than once), then you are virtually guaranteed a crash. Once Release() is called, the pointer becomes invalid. When you release a COM object, you should always do:
if (g_pd3dDevice != NULL){    g_pd3dDevice->Release();    g_pd3dDevice = NULL;}


Also, if you don't initialize your global pointer to NULL explicitly, then there is no guarantee that the pointer will be NULL when you check it. So, if your device creation fails, your pointer may still have garbage in it that would lead to a crash when you try to release it.

edit - yeah. what JoeyBlow2 said.
Oh sorry for being so clumsy, I did have a g_pd3dDevice = NULL; line right where you put it and I inited the pointer to NULL. The error is something else :(.

Some further testing suggests that it doesn't occur when I'm rendering just a few faces at the time, like a square, but when I fill a vertex and index buffer with more, say 16 vertices and 18 indices, and call DrawIndexedPrimitive, I get this error when I quit the app (and the vertices don't appear). Maybe this could be the cause of it? But isn't it pretty usual for a game to render something as little as 16 vertices?
Are you sure your index and vertex buffers are large enough to hold the vertices? Are you passing the correct number of primitives (not vertices) to DrawIndexedPrimitive?
Yes, I'm pretty sure of that. Should I post the code?

Anyway, from what I understand there are things that can screw up the device, so that later when you try to release it it crashes the app... Maybe I should try sending a smaller number of vertices and indices to the renderer, unless someone who has made an app that sends 18 or more can confirm that it should be possible with most hardware... Anyway I'll doublecheck the code and post it if it's helpful in any way.
I've sent 4 million indices to the device before... While the driver chokes on it (by severe stuttering), it doesn't blow up in the release.

I'm thinking (as Dave is) that you might be clobbering some memory with your pointers with the Vertex/Index buffers, so please post it.
I also get this when I declare textures or sprites or some other objects that use the Device object... I pass my Device around so that I am sure to only create one device... but when the device is lost, if an object is not destroyed before the device is lost, I get an OnDeviceLost event... and that's when it crashes.

This is easily remedied by insuring that you dispose of any resources obtained in your program when you are done with them... before releasing your device.

You can check to see if you're getting this sort of problem by checking the Output window of your application after the crash... first, you can stop the debugger before you call those methods... usually it won't crash when you go through them line by line until afterwards when you continue out of the function and the program begins to clean up resources. Anyway, that is a possibility.

-E
Enoch DagorLead DeveloperDark Sky EntertainmentBeyond Protocol
Quote:Original post by JoeyBlow2
I'm thinking (as Dave is) that you might be clobbering some memory with your pointers with the Vertex/Index buffers, so please post it.


Ok, the problem is my code is a mess at the moment... I'll try rewriting it and if the problem remains I'll repost it. Your suggestion seems likely, because I'm algorithmically creating the vertices and faces from a few variables...

This topic is closed to new replies.

Advertisement