Advertisement

Delete operator and Debug Assertion errors

Started by August 05, 2000 11:29 PM
3 comments, last by SeanHowe 24 years, 4 months ago
I seem to be getting a debug assertion error (not compiler error, error pops up when I run the program) when using the delete operator on a dynamically allocated node of a linked list. I ran the debugger and I know that the node is a valid pointer. I think it might have something to do with the fact that I declare the list in a program but modify it in a function in a run-time linked DLL. Here''s the exact Debug assertion error: Debug Assertion Failed! Program: ...TESTAPP.EXE File: dbgheap.c Line: 1076 Expression: _pFirstBlock == pHead I''m really stumped on this one, any ideas on why this is happening are welcome.
I don''t know if this will help but I get this kind of error if I try to delete memory that wasn''t allocated with new. If you''ve deleted it already, this could cause a problem. I don''t know much about run-time libraries but I can''t see that causing a problem.

Hope that helps.

Advertisement
when you delete something the pointer itself still points to the same memory location, so after deletes you should always set the pointer to null:

int* pInt = new int;
delete pInt;
pInt = NULL;

delete pInt; //ok - delete doesnt blow up if given a null pointer, it just quietly fails

quote:
I get this kind of error if I try to delete memory that wasn''t allocated with new


so how is it allocated? more info please
You might try using free() on the memory. Also, like the others said, a pointer might still look like its valid even though its not. Make sure you set pointers to NULL when you delete them.


----------------------------------------
Who is this General Failure and why is he trying to read my hard disk?

ian@gdnmail.net
I got the same type of error when I tried to delete a ''new''ed array. ie.

int * a;

a = new int[10];

delete a; // wrong

delete [] a; // right

Alan
"There will come a time when you believe everything is finished. That will be the beginning." -Louis L'Amour

This topic is closed to new replies.

Advertisement