Danny
Danny

Reputation: 9614

Deleting an object when multiple pointers are pointing to it?

I've been told that when if I have multiple pointers pointing to the same object, I cannot delete it normally (using the delete keyword). Instead, I've been told that I need to set the pointers to NULL or 0.

Given I have:

ClassA* object = new ClassA();
ClassA* pointer1 = object;
ClassA* pointer2 = object;

So to delete pointer1 and pointer2, do I need to do the following?

pointer1 = 0;
pointer2 = 0:

Once I've set it to NULL, do I still need to use the keyword delete? Or is just setting it to 0 good enough?

Upvotes: 8

Views: 13570

Answers (7)

Pete Becker
Pete Becker

Reputation: 76235

The code creates the object with new; it should destroy the object with delete. Once. The fact that there are other pointers to the object doesn't matter, nor does it matter whether the code sets those pointers to 0. (Unless you're running with a garbage collector, which you're not doing). The only benefit from setting those pointers to 0 is if your program uses 0 as a flag that means no object. That's a design decision, and whether it's appropriate can't be answered from the information presented here.

Upvotes: 0

Aesthete
Aesthete

Reputation: 18850

Whenever you new an object, you need to delete it, free'ing the memory

ClassA* object = new ClassA();

delete object; // Free's the memory you allocated.

The point of setting your pointers to NULL is to stop dereferencing pointers that are invalid

object = NULL;

This is done so that tests can be performed before attempting a dereference:

if(object != NULL)
{
  object->SomeMethod(); // We can assume it's safe to use the pointer.
}

Also note that you can delete the memory from any pointer that points to it.

ClassA* object = new ClassA();
ClassA* pointer1 = object;
ClassA* pointer2 = object;

delete pointer1; 

object, pointer1, and pointer2 now all point to memory that has already been released, and unless they will be redefined, they should all be set to NULL.

Upvotes: 9

Neel Basu
Neel Basu

Reputation: 12894

Its not You cannot delete. But you should not. because deleteing an already deleted oibject will crash your program. and you will see glibc detected double free. So the best practice if

if(object){
   delete object;
   object = 0;
}

If you don't set it to 0 or NULL and gaurd it win an if you may double delete it.

Upvotes: 0

Christian Stieber
Christian Stieber

Reputation: 12496

You just have to make sure that you don't use the object anymore after deleting it. It doesn't matter whether you have pointers pointing to it, but you need to know that these pointers are now invalid. Setting them to nullptr is merely a way to get a sure crash when you use them; using invalid pointers may or may not cause a crash, but it will eventually cause obscure problems that are difficult to trace.

However, if you have "live" pointers to the object when you delete it, that MIGHT indicate a problem in your code. Usually, an object gets deleted when nobody needs it anymore, which means there SHOULD be no pointers or other references to it. This is not a hard rule, of course.

Upvotes: 2

Alok Save
Alok Save

Reputation: 206508

The rule is You must call delete as many times you called new.
So if you allocated memory only once you need to deallocate it only once too.

You can just be better off using shared_ptr to avoid this manual memory management, wherein the smart pointer itself will deallocate the memory once no pointer is pointing to it.

Upvotes: 4

elyashiv
elyashiv

Reputation: 3691

you have to use delete other wise you well have a memory leak

Upvotes: 1

user1590232
user1590232

Reputation:

Nope. You need to delete it and set to 0 after that, or use smart pointers

Upvotes: 1

Related Questions