Jump to content
  • Advertisement
Sign in to follow this  
Mike.Popoloski

delete

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

Advertisement
If you change the pointer position.
Ex.:
int *p = new int[4];
p++;
delete p; // FAIL because p doesn't point on the allocation address

Share this post


Link to post
Share on other sites
Quote:
Original post by ussnewjersey4
Is there a reason why delete would fail? As in crash? Besides the object its trying to delete is null.


Deleting a null pointer does not cause a program to crash, it's actually a perfectly acceptable operation (which just does nothing). What can potentially crash your program (causes undefined results) is deleting something not allocated by new, deleting something with delete that was allocated with new[], deleting something with delete[] that was allocated with new, deleting something with delete that was allocated with malloc, deleting something that was already deleted, deleting an uninitialized pointer, etc.

Share this post


Link to post
Share on other sites
If the pointer is null, delete is a no-op, so that wouldn't cause a crash. What can cause a crash is if you are double deleting, deleting something that didn't come from new, deleting something with a pointer to the wrong type, deleting something with a pointer to a base type if there is no virtual destructor, if the heap was corrupted through a buffer overrun or calling delete when you needed delete[] or vice versa. There are probably more possible causes too.

Share this post


Link to post
Share on other sites
Delte wont doing do anything on a NULL pointer. As for crashing, or you sure it's NULL and are your sure delete's the problem. Might be something with a desconstructor. It would be a lot easier to tell if you either post the code, or give an an example of code with the problem.

Share this post


Link to post
Share on other sites
OK. Thanks for the fast replys. First of all, I am 100% positive it is being called by new and am 100% positive it is not null. Here is a sample of the code.

-- Create Function -- ( DEVICE **device )

*device = new DEVICE();

-- Release Function --

if( (*device) != NULL )
{
MessageBox(NULL,"Test1",....);
delete (*device);
(*device) = NULL;
MessageBox(NULL,"test2",....);
}

-- Deconstructor --
MessageBox(NULL,"Test3",......);

The reason I know its delete is because I get the test1 box, but not the test2 or 3 boxes.

Share this post


Link to post
Share on other sites
No offense, but using MessageBox is not an effective or by any stretch of the imagination infallible way of debugging programs. It does not always display when you think it displays; in fact, the more complex your programs become, the less often it does. In this particular instance, it may or may not be happening the way you expect it to, but it's always a good idea to learn how to use your debugger for backtracing and watching variables and such.

Cheers,
Twilight Dragon

Share this post


Link to post
Share on other sites
Are you sure (*device) in your release function is the same as the one that gets created in the create-function?
The double-pointer approach enables you (or library functions) to do some pretty shady things...

EDIT: .. oh and, I second TwilightDragon. A debugger can let you pinpoint exactly what is happening.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!