Getting access violations when using class inheritance with cleanup

Started by
13 comments, last by johnnyBravo 20 years, 7 months ago
Im creating a direct3d wrapper. I''ve got classes for each section such as Text, Sprites, Textures, Meshes etc. Each class has the inheritance of D3DDevice class. Which has a cleanup destructor for the "device" It also contains the "LPDIRECT3DDEVICE9 device" I have all the declarations of text, sprite etc in a main class called "Direct3D" so i can call for example:

Direct3D Graphics;
Graphics.Text.Draw("hi",2,2);
The problem is, when i have more than one class declared in "Direct3D", it has access violation when i end my program. I assume this from testing, that it is because I have a number of deconstructors for cleaning up "device". Like one in "Text", another in "Meshes"; So when I close my program it runs a deconstructor in each class, that is Texture, Meshes. When there is only one, there is no problem, but when there is 2 or more, i get the violation. By the way Direct3D also inherits D3DDevice class and also creates the Device. THanks.
Advertisement
Hard to tell what the problem is without knowing whats in the destructors (not deconstructors) and how you''re initialising the device etc.
CYer, Blitz
quote:Original post by johnnyBravo
When there is only one, there is no problem, but when there is 2 or more, i get the violation.


What do you mean by "only one"? How are you storing your objects? Are you using linked lists, arrays?

Give some source, at least the part that trows the error when you debug. Or more pseudo.
[size="2"]I like the Walrus best.
Ok heres the class that all my classes are inheriting from
class D3DDevice {public:	LPDIRECT3DDEVICE9 myDevice;		~D3DDevice() {		if( myDevice != NULL) 			myDevice->Release();		}};


this is my Common class that has render etc, inherits from D3Ddevice
  class D3DCommon:public D3DDevice {public:	void BeginScene(DWORD bgColour, DWORD fgColour)	{		myDevice->SetRenderState( D3DRS_AMBIENT, fgColour );		myDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, bgColour, 1.0f, 0);		myDevice->BeginScene();	}	void EndScene()	{		myDevice->EndScene();		myDevice->Present(NULL, NULL, NULL, NULL );	}};



and heres my mainish class that declares D3DCommon, creates the device etc. Also inherits D3DDevice so it can use the myDevice to create it...Like put a value in it etc and use it.
class Direct3D:public D3DDevice {private:	LPDIRECT3D9	myD3D; public:	~Direct3D()	{		if( myD3D != NULL )			myD3D->Release();	}	D3DCommon Common;	D3DText Text;		void GetDevice()	{		Common.myDevice= myDevice;	}	void Init(HWND hWnd)	{		myD3D = Direct3DCreate9( D3D_SDK_VERSION );    		D3DPRESENT_PARAMETERS d3dpp; 		ZeroMemory( &d3dpp, sizeof(d3dpp) ); 		d3dpp.Windowed = TRUE; 		d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;		d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; 		d3dpp.EnableAutoDepthStencil = TRUE;		d3dpp.AutoDepthStencilFormat = D3DFMT_D16;		myD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &myDevice ) ;		myDevice->SetRenderState( D3DRS_LIGHTING, TRUE );		GetDevice();	}	};   


[edited by - johnnyBravo on September 2, 2003 7:21:57 AM]

[edited by - johnnyBravo on September 2, 2003 7:22:42 AM]
Oh i mean if i declare the things like D3DCommon or D3DText

or like what ive got posted now, Direct3D and D3DCommon.

Direct3D without the D3DCommon declared works fine
but when D3DCommon is declared inside of Direct3D i get the violation.
============================
Before that I had it like Direct3D didnt inherit the D3DDevice

and the Init for the device was in D3DCommon.

And when i just had D3DCommon then it worked. As there was only one class actively using the inheritance as the others wern''t declared. But when i declared D3DText aswell it had the violation as there was more than one.

I''m telling you this so you don''t jump to the conclusion that having a class inherit something then a nested class also inheriting the same thing causes this.
It's hard to me to understand what your saying. Maybe you're releasing your objects more than once? Try seting your objects to NULL after releasing them in the destructor/s.

EDIT:

I mean:
   if( myDevice != NULL)   {      myDevice->Release();      myDevice = NULL;   } 


[edited by - owl on September 2, 2003 7:48:40 AM]
[size="2"]I like the Walrus best.
I''ve added that but it still has the access violation.
well, access violation means that you're trying to access an invalid memory location. This usually happens when you are accessing or deleting an invalid pointer.

It would be very useful if you could press F8 in the first line of all destructors and then run your code and see what line is causing the error. It looks like the destructor is doing something to some pointer there, and then another class is trying to do something with that very same pointer.

edit: ?

[edited by - owl on September 2, 2003 8:20:02 AM]
[size="2"]I like the Walrus best.
ah i might have an idea what that would be!

I think the main class Direct3D is being deleted first, as it holds the other classes declarations in itself.

Then maybe the destructors are still trying to delete the myDevices from common etc but they are not there anymore?

Could this be it?
There is only one way to know that
Check it out and tell me.
[size="2"]I like the Walrus best.

This topic is closed to new replies.

Advertisement