Strange problem with delete with VS 2012.

Started by
12 comments, last by BitMaster 10 years, 10 months ago

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?

Advertisement

Dunno where you read that. Deleting the same pointer twice (without reallocating with new inbetween) is an error.

You probably read that deleting a nullptr (0, or NULL) is safe, that is correct, but not a good way of preventing bugs.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

According to everything I have read, I should be able to call delete on a pointer more than once without a crash.

You have been reading in the wrong places. As you have just discovered, double delete is a very real programming error.

Attempting to delete a null object results in a nul-op. Basically delete does this:

delete( void* thing)

{

if(thing)

real_delete(thing);

}

If you call delete, the very next line should be to assign the pointer to NULL.

Or you can avoid the problem entirely by using smart pointers and container classes.

No, really. Use smart pointers and container classes. Assume that a naked pointer is a bug in progress.

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.

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"?

if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

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?

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

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.

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).

f@dzhttp://festini.device-zero.de

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.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

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.

This topic is closed to new replies.

Advertisement