delete help

Started by
10 comments, last by phresnel 15 years, 3 months ago
if my program crashes at delete p; where p is my class pointer.. then what could be possible reasons for the crash..??
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.
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
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.
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
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.
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).
NextWar: The Quest for Earth available now for Windows Phone 7.
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?
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).
From my experience the best way to use new/delete is like this:
A *a = NULL;a = new A;if(!a)  error, new failedif(a)  delete a;a = NULL;


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

This topic is closed to new replies.

Advertisement