Advertisement

Deleting Classes

Started by January 09, 2003 08:02 PM
4 comments, last by MattS423 21 years, 10 months ago
Hey guys! I have a class that has an private/protected allocated array in it. I use the destructor to delete it, like this:

CPolygon::~CPolygon(void)
{
      delete [] Verts;
}
 
The destructor is public. anyway.. I use a global varible and then [size=1]new[/size] the class and use the constructor. In my programs exit deal, I have this line:

delete Triangle;
 
Triangle being the class. Now, the problem is that windows gives me a "warning beep" when i exit the program. it makes me think memory leak. is there one?
Programmers of the world, UNTIE!
Not in the code given.

Calling delete on an Object invokes its destructor
Advertisement
the program exit-thingy
int GameShutdown(){	if(lpDDSBackBuffer)		lpDDSBackBuffer->Release();		if(lpDDSPrimary)		lpDDSPrimary->Release();	if(lpDD)		lpDD->Release();	delete Triangle;		return 1;}   




CPolygon constructor & destructor:

      CPolygon::CPolygon(int NumOfVerts, int StartPosx,				   int StartPosy,int StartVelx,				   int StartVely, int InitState,				   DWORD InitColor,VERTEX2D *vList, RECT ClipTo){	NumVerts = NumOfVerts;	xPos = StartPosx;	yPos = StartPosy;	xVel = StartVelx;	yVel = StartVely;	State = InitState;	Color = InitColor;	//init the ClipRect	ClipRect.bottom = ClipTo.bottom;	ClipRect.top = ClipTo.top;	ClipRect.left = ClipTo.left;	ClipRect.right = ClipTo.right;	//init the verts.	Verts = new VERTEX2D[NumVerts];	int Counter = 0;	for(Counter = 0; Counter <= NumVerts; Counter++)	{		Verts[Counter].x = vList[Counter].x;		Verts[Counter].y = vList[Counter].y;	}	//thats good...now...	//...we're done!}CPolygon::~CPolygon(void){	delete [] Verts;}      




init of Triangle in my program:
CPolygon *Triangle;...VERTEX2D VertList[3];		VertList[0].x = 0;	VertList[0].y = 0;	VertList[1].x = 5;	VertList[1].y = 0;		VertList[2].x = 0;	VertList[2].y = 5;	RECT CRect;	CRect.bottom = SCREEN_HEIGHT;	CRect.left = 0;	CRect.right = SCREEN_WIDTH;	CRect.top = 0; 	Triangle = new CPolygon(3,400,300,0,0,NULL,				_RGB32BIT(0,255,255,255),VertList,CRect);	return 1;   


thanks for your help!

[edited by - MattS423 on January 9, 2003 10:11:30 PM]

[edited by - MattS423 on January 9, 2003 10:12:37 PM]
Programmers of the world, UNTIE!
I''m not sure what the problem is, but to guard against access violations, make sure you set Vertices to 0 in the constructor, and check that it''s not 0 when you delete[] it (otherwise you''re trying to delete memory that never got allocated).

Try running the program through the MSVC debugger and see if it gives you any error messages when you shut the program down.

Superpig
- saving pigs from untimely fates, and when he''s not doing that, runs The Binary Refinery.

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

quote:
Verts = new VERTEX2D[NumVerts];

int Counter = 0;
for(Counter = 0; Counter <= NumVerts; Counter++)

MEEEP. Access violation (or memory corruption)

You are reserving NumVerts entries in the array, but you are writing to NumVerts+1.
Also, be careful not to confuse classes and objects . A class is a type, like an int or a char. An object is a specific instance of that class. You never delete classes, only objects.

Anyways, change the code Yann L mentioned to this:

Verts = new VERTEX2D[NumVerts];
for(int Counter = 0; Counter < NumVerts; Counter++)
/* ... */

-Mike

This topic is closed to new replies.

Advertisement