Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Strange problem with delete with VS 2012.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
13 replies to this topic

#1 MarkS   Prime Members   -  Reputation: 887

Like
0Likes
Like

Posted 29 May 2013 - 03:49 PM

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=auto:0]

~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, 29 May 2013 - 03:50 PM.


Sponsor:

#2 Paradigm Shifter   Crossbones+   -  Reputation: 5438

Like
6Likes
Like

Posted 29 May 2013 - 03:55 PM

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

#3 frob   Moderators   -  Reputation: 22770

Like
7Likes
Like

Posted 29 May 2013 - 03:58 PM

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.


Edited by frob, 29 May 2013 - 03:59 PM.

Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I write about assorted stuff.


#4 MarkS   Prime Members   -  Reputation: 887

Like
0Likes
Like

Posted 29 May 2013 - 04:04 PM

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.



#5 ChaosEngine   Crossbones+   -  Reputation: 2502

Like
3Likes
Like

Posted 29 May 2013 - 05:27 PM

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

#6 Khatharr   Crossbones+   -  Reputation: 3040

Like
0Likes
Like

Posted 29 May 2013 - 06:38 PM

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.

#7 MarkS   Prime Members   -  Reputation: 887

Like
0Likes
Like

Posted 29 May 2013 - 07:01 PM

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.



#8 Trienco   Crossbones+   -  Reputation: 2224

Like
2Likes
Like

Posted 29 May 2013 - 11:32 PM

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

#9 NightCreature83   Crossbones+   -  Reputation: 3033

Like
0Likes
Like

Posted 30 May 2013 - 01:36 AM

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, 30 May 2013 - 01:37 AM.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, Mad Max

#10 BitMaster   Crossbones+   -  Reputation: 4435

Like
2Likes
Like

Posted 30 May 2013 - 02:11 AM

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, 30 May 2013 - 02:12 AM.


#11 MarkS   Prime Members   -  Reputation: 887

Like
0Likes
Like

Posted 30 May 2013 - 07:19 PM

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, 30 May 2013 - 07:19 PM.


#12 swiftcoder   Senior Moderators   -  Reputation: 10396

Like
4Likes
Like

Posted 01 June 2013 - 12:28 PM

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


Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#13 MarkS   Prime Members   -  Reputation: 887

Like
0Likes
Like

Posted 02 June 2013 - 03:26 PM

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, 02 June 2013 - 03:30 PM.


#14 BitMaster   Crossbones+   -  Reputation: 4435

Like
0Likes
Like

Posted 04 June 2013 - 05:15 AM

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, 04 June 2013 - 05:16 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS