destructor?

Started by
6 comments, last by SiCrane 16 years, 3 months ago
how do destructors work as i create a local object instead of a global object and my destructor gives me errors. to fix this i create another function which i call to clean up code and now it works . i create the object camera cam; camera::~camera(){ //if(g_font2){ // g_font2->Release();//error access violation // g_font2=NULL; // } } void camera::shutdown() //newly created and now it all works { if(g_font2) { g_font2->Release(); g_font2=NULL; } }
Advertisement
Post code.
what code to you need as i gave all relevant parts. i didnt give fuction declarations but they are too simple to give

eg in .h ~camera();
void shutdown(void);

or call cam.shutdown();
Quote:Original post by jagguy2
what code to you need as i gave all relevant parts. i didnt give fuction declarations but they are too simple to give

eg in .h ~camera();
void shutdown(void);

or call cam.shutdown();



Nope. We have no idea where the camera is initialised, what or where g_font is etc. This is the information we need.
all in the same winMain function

int WINAPI WinMain(	HINSTANCE hInstance,					HINSTANCE hPrevInstance,					LPSTR     lpCmdLine,					int       nCmdShow ){	WNDCLASSEX winClass; 	MSG        uMsg;	HRESULT hr;	camera cam;........	if (FAILED (hr=D3DXCreateFont(g_pd3dDevice, 20,0,FW_NORMAL, 1,					false,DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, 					ANTIALIASED_QUALITY,DEFAULT_PITCH|FF_DONTCARE,					L"Arial",&g_font))  )  	MessageBox(NULL,L"User Abort",L"font error",MB_OK);......    render(cam);		}	shutDown();	cam.shutdown();

PS the code does work if i dynamically allocate space for 'cam' as

in winMain

camera *cam= new camera;
//camera cam;

i then use delete and my destructor works correctly.
Your code still isn't clear to me. Can you fix the formatting so that it's immediately clear how things are lined up and what code is part of what code block? Also, where is g_font declared? And what is g_font2?
When you have a problem like this, one thing you can try is putting together a minimal, but complete, source dump of the issue. That is, try to create a program that misbehaves in exactly the same way. One way to do that is to create a new project and add to it until you get the problem. Another way is to create a copy of your existing project and start deleting code until you can't delete anymore and still have the same problem. Doing so may give you a better understanding of the issue. At the very least, it gives you something useful to post.

This topic is closed to new replies.

Advertisement