Jump to content
  • Advertisement
Sign in to follow this  
y2jsave

delete help

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

if my program crashes at delete p; where p is my class pointer.. then what could be possible reasons for the crash..??

Share this post


Link to post
Share on other sites
Advertisement
Code of the problem would help, but I'm guessing that you’re probably trying to delete a pointer which no longer points to valid memory.

Other possibilities could also include trying to delete a class object on the stack. Pointers only work properly if they point to objects created on the heap (not the stack).

But as I said... With no code submitted it could be a whole host of problems.

Share this post


Link to post
Share on other sites
it must be a wrong pointer. it maybe did not be initialize well. or have been delete already, like this:

int * pData = new int;
delete pData;
delete pData; // crash here

Share this post


Link to post
Share on other sites
According to the MSDN the delete operator works fine if a new fails. A failed new sets the pointer to 0 and deleting a pointer set to 0 is safe, by standard. The same article says that the value of a deleted pointer is undefined. This makes it dangerous to delete it again because deleting an undefined/potentially random value can have interesting results (like your crash).

To prevent this either don't use pointers directly (research smart pointers) or set the value of the pointer to 0 after you delete it.

Disclaimer: I am not sure how accurate the link is as far as standards go, and I don't have my C++ reference handy. That means if you're not using MSVC++ this information could be completely wrong and, in fact, the opposite might be true.

Share this post


Link to post
Share on other sites
i have a doube
suppose my class name is A

if i use like this

A * s = NULL

and after doing something with it

i had done
delete a;

it crashed

but if i replace it with free s .. it worked fine ..

can someone tell why it happened

Share this post


Link to post
Share on other sites
Based on your incorrect example code I would have to say your biggest fallacy is failing to allocate an instance of class A in the first place. However that isn't very useful considering you still haven't given us the code that is giving you the problem.

Share this post


Link to post
Share on other sites
Quote:
Original post by binchawpz
According to the MSDN the delete operator works fine if a new fails. A failed new sets the pointer to 0 and deleting a pointer set to 0 is safe, by standard. The same article says that the value of a deleted pointer is undefined. This makes it dangerous to delete it again because deleting an undefined/potentially random value can have interesting results (like your crash).

To prevent this either don't use pointers directly (research smart pointers) or set the value of the pointer to 0 after you delete it.

Disclaimer: I am not sure how accurate the link is as far as standards go, and I don't have my C++ reference handy. That means if you're not using MSVC++ this information could be completely wrong and, in fact, the opposite might be true.

It's wrong even for MSVC IIRC. MSVC's implementation of new since VC6 conforms to the standard: it throws std::bad_alloc. So it won't set the value of the pointer to 0 (it can't since new never returns if an exception is thrown).

Share this post


Link to post
Share on other sites
Quote:
Original post by Sc4Freak
It's wrong even for MSVC IIRC. MSVC's implementation of new since VC6 conforms to the standard: it throws std::bad_alloc. So it won't set the value of the pointer to 0 (it can't since new never returns if an exception is thrown).


Huh, that's kind of interesting considering that specific documentation page is supposed to be for MSVC++ 2k8. So much for keeping documentation up to date. Maybe that is specific to when exceptions are turned off?

Share this post


Link to post
Share on other sites
Quote:
Original post by binchawpz
Huh, that's kind of interesting considering that specific documentation page is supposed to be for MSVC++ 2k8. So much for keeping documentation up to date. Maybe that is specific to when exceptions are turned off?
The documentation appears to be correct. It states:
Quote:
You can, however, use delete on a pointer with the value 0. This provision means that, when new returns 0 on failure, deleting the result of a failed new operation is harmless.
This does not imply that new returns 0 on failure as the normal behavior, it merely describes what happens in those situations when new is made to return 0 on failure (for instance, by using a no-throw version).

Share this post


Link to post
Share on other sites
From my experience the best way to use new/delete is like this:

A *a = NULL;
a = new A;
if(!a)
error, new failed



if(a)
delete a;
a = NULL;



The possible causes for a crash at delete is that new failed or the pointer already has been deleted.

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!