• Advertisement
Sign in to follow this  

Strange problem with delete with VS 2012.

This topic is 1690 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:

 

[code]

~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

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.

 

That, sadly, describes me. However, I am working to rectify that.

 

I implemented smart pointers in my code in just a few minutes and I have to say that I like them. Once I got past the weird initialization, compared to just calling new on a raw pointer, it actually makes sense and is much more convenient than raw pointers.

Edited by MarkS

Share this post


Link to post
Share on other sites

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.

Sadly, there is now also a breed of programmer who have learned that "a raw pointer is a bug in progress", but only learned to use a single smart_ptr class.

 

It bugs me no end to see an externally-reference-counted smart pointer implementation being used to scope non-shared instance variables...

Share this post


Link to post
Share on other sites

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.

Sadly, there is now also a breed of programmer who have learned that "a raw pointer is a bug in progress", but only learned to use a single smart_ptr class.

 

It bugs me no end to see an externally-reference-counted smart pointer implementation being used to scope non-shared instance variables...

That is what I was originally thinking and one reason that I did not use smart pointers. The class and associated member pointer is not shared. However, I suppose this is the purpose of unique_ptr, which is what I used when I converted my code to use smart pointers.

 

Raw pointers are not evil, they just complicate memory management. In fact, I would say this push towards using smart pointers in exclusion to raw pointers is creating a generation of programmers that do not understand how to do proper memory management, i.e., the compiler does it for me, so why should I care how to do it?

Edited by MarkS

Share this post


Link to post
Share on other sites

Sadly, there is now also a breed of programmer who have learned that "a raw pointer is a bug in progress", but only learned to use a single smart_ptr class.
 
It bugs me no end to see an externally-reference-counted smart pointer implementation being used to scope non-shared instance variables...

Well, pre-C++11 there was not really a good way to deal with non-shared managed pointer. std::auto_ptr was not up to the job in most cases. boost::scoped_ptr was noncopyable (although swappable) which made quite a few potential uses annoying and/or awkward.

I'm a bit disappointed though that something like boost::intrusive_ptr didn't make it into C++11. Edited by BitMaster

Share this post


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

  • Advertisement