Jump to content
  • Advertisement
Sign in to follow this  
MarkS

Strange problem with delete with VS 2012.

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

According to everything I have read, I should be able to call delete on a pointer more than once without a crash. I recently ran into an issue with Visual Studio where the destructor was called more than once. That was due to a problem in my code and was corrected, but oddly, this caused a crash. If I did this, there was no crash:

 

~destructor()

{
if(pointer != NULL)

{

delete pointer;

pointer = NULL;

}
}

[\code]

 

This isn't supposed to be necessary, correct? Is Visual Studio screwed up or is this supposed to happen?

Edited by MarkS

Share this post


Link to post
Share on other sites
Advertisement

This was a bug that presented itself during a call to a copy constructor that was copying image data contained in a pointer of unsigned chars. I use container classes, but smart pointers are a little overkill for this application.

 

Thanks for the info. I misunderstood. I failed to set the pointer to NULL after the call to delete.

Share this post


Link to post
Share on other sites

I use container classes, but smart pointers are a little overkill for this application.

 

No, they're really not. Seriously, whenever you find yourself writing a delete in a destructor, stop, and use a smart pointer instead.

 

The overhead is minimal and in some cases non-existent, the code is more intention revealing (the pointer type will tell you the ownership semantics), and you don't have to remember to delete them.

 

It's a win win situation.

 

Put it this way, if I asked you to hang up a picture, would you say "it's just one nail, a hammer is overkill here, I'll just hit it with my forehead"?

Share this post


Link to post
Share on other sites

This was a bug that presented itself during a call to a copy constructor that was copying image data contained in a pointer of unsigned chars. I use container classes, but smart pointers are a little overkill for this application.

 

Thanks for the info. I misunderstood. I failed to set the pointer to NULL after the call to delete.

 

Pointers do not contain unsigned chars or image data. Pointers point to a location in memory. /pedantic

 

More importantly, what is going on in your program that your structure is encountering objects that may or may not be deleted?

Share this post


Link to post
Share on other sites

This was a bug that presented itself during a call to a copy constructor that was copying image data contained in a pointer of unsigned chars. I use container classes, but smart pointers are a little overkill for this application.

 

Thanks for the info. I misunderstood. I failed to set the pointer to NULL after the call to delete.

 

Pointers do not contain unsigned chars or image data. Pointers point to a location in memory. /pedantic

 

More importantly, what is going on in your program that your structure is encountering objects that may or may not be deleted?

Yeah, I mistyped. :o I was remembering the code (unsigned char *image_data) as I typed and typed that.

As to the question, I am going from memory and I don't remember exactly what was going on. I do know that the code was structured badly and I corrected it. My confusion was in not remembering that it is OK to delete a pointer set to NULL. I thought it was OK to delete a pointer more than once.

To be honest, I have always used raw pointers for image data. I really never gave smart pointers much thought. I'll experiment with them tonight.

Share this post


Link to post
Share on other sites

In case "pointer" is a member of the object, I would be extremely worried if the bug disappears, just because it's set to null after delete. In fact, I would strongly advice _against_ setting member pointers to null in your destructor, exactly because it can hide more severe bugs. Doing so should have absolutely zero effect on program execution, unless something else is wrong (like deleting the actual object twice). In that case, reliably crashing is the best that can happen, as it clearly shows that something is wrong (instead of causing weird and inexplicable behavior down the road).

Share this post


Link to post
Share on other sites

I use container classes, but smart pointers are a little overkill for this application.

 

No, they're really not. Seriously, whenever you find yourself writing a delete in a destructor, stop, and use a smart pointer instead.

 

The overhead is minimal and in some cases non-existent, the code is more intention revealing (the pointer type will tell you the ownership semantics), and you don't have to remember to delete them.

 

It's a win win situation.

 

Put it this way, if I asked you to hang up a picture, would you say "it's just one nail, a hammer is overkill here, I'll just hit it with my forehead"?

 

Whilst I agree that smart pointers clear up the semantics of the code, in real production code I have not really seen them in use so far sadly though.
 

Edited by NightCreature83

Share this post


Link to post
Share on other sites

That's often one of two problems. First, code bases have a tendency to be ancient. std::auto_ptr was often a heptagon in a world of mainly round and square holes. Template support was very shaky on early compilers (and still is on some platforms). Boost was not always an option.

 

Secondly, a sadly high number of 'C++ programmers' have decided to call themselves that after reaching the 'C with classes' state and never evolved from there.

Edited by BitMaster

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!