Getting access violations when using class inheritance with cleanup

Started by
13 comments, last by johnnyBravo 20 years, 7 months ago
Nope that aint it.

I tried doing this after a bunch of other things
void Unint() {if( Common.myDevice != NULL) 	Common.myDevice->Release();if( Text.myDevice != NULL) 	Text.myDevice->Release();if( myDevice != NULL) 	myDevice->Release();}

and called uninit before the program ended.

But still getting the same problem, i removed any 2 of the releases(tried different combinations) and it worked.

Could it be that there is really only one myDevice allowed to be called, and the rest are something like pointers. So when one is released, the memory spot is cleared so the rest can find it?

Like the device can have different values etc, but the Release command just releases it all.

[edited by - johnnyBravo on September 2, 2003 9:03:48 AM]
Advertisement
Common.myDevice and myDevice are probably pointing to the same memory location, and thus they are pointing to the same object. You are calling Release twice(*) for the same object. After the first(**) call to Release the object is destroyed and any further calls will result in an access violation.

* Calling it twice is ok if you have called AddRef when doing the copy.

** Unless you have called AddRef when doing a copy.

So: Call AddRef when doing copies of the pointers and always call Release when you are done with the pointer.

[How To Ask Questions|STL Programmer''s Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
You should release/delete your objects in the same class you create them, and only there. When you create an object and you set myDevice to point to it, a number is assigned to myDevice which represents the memory location of that object you created. When you release the object, the object stops to exist, but the pointer may still have some value different from NULL, so checking myDevice!=NULL will be true even when the object was already released.

Again, you NEED to DEBUG your app to KNOW EXACTLY which pointer is getting messed up. I encourage you to find a way to debug your app and see what''s going on.
[size="2"]I like the Walrus best.
I can''t understand why all classes inherit from D3DDevice..
I think the proper way would be:

class D3DDevice{
...
}


class D3DCommon{
...
D3DDevice renderer
...
D3DCommon(device):renderer(device){};
}

etc..

I think now all your classes share the device but they still seem to have different device-objects..
--------------------------------------------------------I'm not crazy, It's the TV that's crazy. Aren't you, TV?--------------------------------------------------------
Owl i was release etc objects in their own classes at first place until i changed it trying different ways, i even tried putting cleanup in each one and doing it, but same thing.


pothead im doing this because i dont want to use direct3d all the time. like im making this a dx wrapper header.

so i call what i want etc.

All the devices in the end equal the one thing in the end,

Its cuz the way c++ has its classes..
So i guess its all getting cleared up with one release.
Thanks for the help

edit:
I know this must look like a total newbie attempt at something halfbaked, but i can't think of any other way of doing this.

That is making easier functions for the dx commands.

Like say sometimes i just want to use Direct Input for the keyboard.
Then other times i might want to use D3D with only basic vertices and Direct Input mouse.

I want to do this without declaring anything, but I want all the methods there incase i do.
With all this stored in a header file.

If anyone knows a better solution to this, I'd be really really grateful.

[edited by - johnnyBravo on September 2, 2003 9:55:24 AM]

[edited by - johnnyBravo on September 2, 2003 9:56:15 AM]

[edited by - johnnyBravo on September 2, 2003 9:57:08 AM]

This topic is closed to new replies.

Advertisement