deleting pointers:debug assertion failed

Started by
7 comments, last by Freaker13 20 years, 11 months ago
Hi all, When i delete pointers i get the following run-time error: debug assertion failed! file dbgdel.cpp Expression:_BLOCK_TYPE_IS_VALID(pHead->nBlockUse) this is my code:
      
sData()
{
	totalTime = time = aantal = 0;
	name = NULL;
	timer = NULL;
}
~sData()
{
	delete name;
	name = NULL;
	delete timer;
	timer = NULL;
}
}sData;

static sData  *data = NULL;

void cProfiler::free()
{
     delete []data;
     data = NULL;
     delete path;
     path = NULL;
}
    
Can someone explain me what i'm doing wrong? thx in advance [edited by - Freaker13 on May 2, 2003 6:48:57 AM]
Advertisement
You''re either
1) deleting something which wasn''t new''ed
2) deleting something which was allready delete''ed
"THE INFORMATION CONTAINED IN THIS REPORT IS CLASSIFIED; DO NOT GO TO FOX NEWS TO READ OR OBTAIN A COPY." , the pentagon
Seems like you''re deleting an uninitialized (or already deleted) pointer.
''path'' isn''t initialized in your code-snippet. Otherwise I can''t see anything wrong..
(and it isn''t strictly necessary to set pointers to NULL in a deconstructor, as the variables can''t be referenced afterwards anyway (unless they''re static))

Run the debugger and see exactly what variable is causing the error - it''s probably easier to find the error then..
both pointers give the same error(data and path). they are not deleted and are initialised.
i''ve checked them on NULL pointers now. But i still get the same error.

void cProfiler::free()
{
if(data != NULL)
{
delete []data;
data = NULL;
}
if(path!= NULL)
{
delete path;
path = NULL;
}
}

I've found it:
with strings i didn't use the new keyword like
char* path;
path = "string";

So don't i have to delete those string pointers?



[edited by - Freaker13 on May 2, 2003 7:51:55 AM]
No, you don''t.

Unless you use the new keyword, variables are located on the stack and it is not necessary (or correct) to delete them.

As a rule: one new -> one delete
You don''t have to have the checks for NULL.

delete NULL;

is fine. It does nothing.
I have had this and I memorized that it is virtually ALWAYS when you have a crap ass copy constructor and pass an object by value. Pass by const reference.

This topic is closed to new replies.

Advertisement