Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

johnnyBravo

Getting access violations when using class inheritance with cleanup

This topic is 5431 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!