Archived

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

delete[] problem

This topic is 4947 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

when i call delete[] to free an object i sometimes get a break point and the following in the output wnd HEAP[*.exe]: HEAP: Free Heap block af0fc8 modified at af1004 after it was freed Unhandled exception at 0x77f7f570 in *.exe: User breakpoint. whats this mean?

Share this post


Link to post
Share on other sites
quote:
Original post by Etnu
if(p)
delete[] p;




That won't help. It's a built-in behaviour that delete-ing a NULL pointer is defined. It just works out to a no-op.

When you create the pointer, initialize it to NULL before you allocate it. There's a chance that the code to allocate the array is never running (depends on your program structure), so if it's not initialized to NULL, when you go to delete it, it'll likely be pointing to somewhere you're not allowed to touch.

Also, when you say you're using "delete[] to free an object", are you trying to say "an array of objects"? Because if it's just a heap-allocated object, and not an array of them, then you don't need to use delete[]. Just delete will do. (Edit: This is the case regardless of whether the object has an array in it or not. You use delete[] only on a pointer that was itself allocated via new <type>[]).

Lastly, check the object's destructor. There could be code in there causing the crash.

If none of those work, try giving us some of your code.

-Auron

[edited by - Auron on May 30, 2004 3:12:33 AM]

Share this post


Link to post
Share on other sites
int WINAPI WinMain(HINSTANCE hinstance,HINSTANCE,char*,int)
{
...
Q::q = new Q;
Q::q->Quad;
...
delete Q::q; // error here (only in release mode)

#ifdef _DEBUG
OutAndFreeLeaks();
#endif
return (int)msg.wParam;
}

Share this post


Link to post
Share on other sites
It''s generally adviseable to initialize all pointers to NULL, and then set them to NULL after deleting.

I use the following macros:

#define SAFE_DELETE(p){if(p) delete p; p=NULL;}
#define SAFE_DELETE_ARRAY(p)(if(p) delete[] p; p=NULL;}

Share this post


Link to post
Share on other sites
Yeah, it is pointless, but it sets you up for easier debugging, since if you''re deleting a null pointer, you probably missed something previously.

Share this post


Link to post
Share on other sites