Skip to main content
GameDev.net gamedev.net
🔒 Locked

delete

Started by Mike.Popoloski Sep 1, 2005 at 4:58 PM 11 replies 950+ views
Original Post
Mike.Popoloski
Mike.Popoloski
Is there a reason why delete would fail? As in crash? Besides the object its trying to delete is null.
Mike Popoloski | Journal | SlimDX
widggman
widggman
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


Couvillion
Couvillion
Using "delete" on something that was allocated on the stack, i.e., without using "new" can cause problems. It's bitten me.
~~C--O -
Polymorphic OOP
Polymorphic OOP
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.
dave
dave
Nothing you would need to worry about, why do you ask?
SiCrane
SiCrane
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.
Cocalus
Cocalus
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.
Mike.Popoloski
Mike.Popoloski
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.

Mike Popoloski | Journal | SlimDX
TDragon
TDragon
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
DaBono
DaBono
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.
Polymorphic OOP
Polymorphic OOP
device itself (the pointer to a pointer) is probably not a valid pointer, though it's difficult to tell for sure without more code. Such a problem could cause the results you are seeing. Firstly, (*device) != NULL may "work" even if device doesn't point to a valid device pointer -- the device pointer to which it points may have already gone out of scope or been deleted, or it wasn't properly initialized to begin with, though the former is more likely the case. Even if device doesn't point to a valid DEVICE pointer, it's still often possible to dereference it, even though you have officially have undefined results, since it may still point to a valid location in memory. However, once you try to do something to that pointer, such as attempt to delete it, you're more likely to get drastic effects such as a crash. This explains why the (*device) == NULL doesn't create a visible error whereas delete (*device) does.
Anon Mike
Anon Mike
Heap corruption caused by overwritting buffers is probably the biggest single source of allocators / deallocators crashing.
-Mike
helix
helix
deleting NULL works just fine. Deleting something that's already been deleted has nailed me pretty often.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.