Jump to content
  • Advertisement
Sign in to follow this  
Sir Sapo

deleting an already deleted pointer

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

There is a bug in my code that crashes my computer upon exiting my application. I narrowed the functions that could be causing it down to a destructor. Now here's my question, if I have already deleted a pointer and I am trying to delete it again, will it crash the app(null-pointer or something?). Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
It will cause because double-delete is a serious bug and you won't get a null pointer when you delete a pointer.

Think of pointers like the keys to your appartment in this regard, when you delete them you transfer ownership of them and the estate (the pointed to memory) to someone else (in this case the memory manager) and although you might have a spare key (you still have the pointer value) using it is not only immoral it's highly illegal and punishable (in this case by chrasing your app or spewing garbage over the heap).

edit: to darn slow...

Share this post


Link to post
Share on other sites
Of note is that deleting a NULLed out pointer is OK.

Bad:

foo * f = new foo;
delete f;
delete f;

Okay:

foo * f = new foo;
delete f;
f = 0;
delete f; //no effect

Share this post


Link to post
Share on other sites
If you have a double-delete happening because of a destructor, then perhaps you have not provided an assignment operator (or copy constructor) which allocates and copies data in member pointer variables.
That caused a double-delete for me very recently.
I suggest you read up on the rule of three.
Quote:
Any class that has an explicitly defined destructor, copy constructor or assignment operator generally needs all three.

btw, congrats Chris, on your article being top of the list in my Google search!

Share this post


Link to post
Share on other sites
If you do need an assignment operator then don't forget to deallocate existing data in it, like that article does!
Array& operator= (const Array& a)
{
// delete the old data
delete[] oldData;
// do what we did before
size = a.size;
data = new int[size];
for(int i = 0 ; i < size ; ++i)
{
data = a.data;
}
return *this;
}

Better expressed (with added exception safety) as:
Array& operator= (const Array& a)
{
// use the copy constructor to allocate memory for us
// avoids code duplication
Array tempCopy(a);
// would usually delagate to a swap() member function
std::swap(data, tempCopy.data);
std::swap(size, tempCopy.size);
// tempCopy's destructor calls delete[] on our old memory
return *this;
}

Enigma

Share this post


Link to post
Share on other sites
Enigma - your first version doesn't just have issues with exception-safety. It has problems with self assignment too. If you ever do foo = foo;, which may very well happen when you have a program filled with pointers, the object's internal data gets deleted before the 'copy' happens. Bad. The copy-swap idiom (your second version) is far superior, though it could be improved with a self-assignment check, for performance reasons, if you expect self-assignment to be common enough (frequency of self-assignment * cost of copy > cost of if statement), which is probably often the case (profile!!!).

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
if you expect self-assignment to be common enough... which is probably often the case.


I think you missed a negation in there? o_O

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!