Removing an object when multiple pointers point to it?

I was told that if I have several pointers pointing to the same object, I cannot delete it normally (using the delete keyword). Instead, they told me that I need to set pointers to NULL or 0.

Given that 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 set it to NULL, do I still need to use the delete keyword? Or just set it to 0 good enough?

+6
source share
7 answers

Whenever you are a new object, you need to delete it, freeing memory

 ClassA* object = new ClassA(); delete object; // Free the memory you allocated. 

The point of setting pointers to NULL is to stop dereferencing invalid pointers

 object = NULL; 

This is to ensure that tests are performed before attempting dereferencing:

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

Also note that you can remove 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 already released memory, and if they are not redefined, they should all be set to NULL .

+8
source

Rule: you must call delete as many times as you called new .
Therefore, if you allocated memory only after you need to free it only once.

You can simply use shared_ptr to avoid this manual memory management, in which the smart pointer itself frees up memory as soon as the pointer does not point to it.

+4
source

You just need to make sure that you no longer use the object after deleting it. It doesn't matter if you have pointers pointing to it, but you need to know that these pointers are now invalid. Installing them on nullptr is just a way to get a sure crash when using them; using invalid pointers may or may not crash, but ultimately it will cause unclear issues that are difficult to track.

However, if you have live pointers to an object when it is deleted, this MIGHT indicates a problem in your code. Typically, an object is deleted when it no longer needs anyone, which means that it MUST be pointers or other references to it. Of course, this is not a strict rule.

+2
source

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

+1
source

you must use delete another reasonable, you have a memory leak

+1
source

Its not you cannot delete . But you should not. because deleting an already deleted oibject will crash your program. and you will see glibc detected double free . Therefore, it is best if

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

If you don't set it to 0 or NULL and gaurd, it wins if , you can remove it twice.

0
source

The code creates an object with a new one; he must destroy the object with deletion. Once. The fact that there are other pointers to the object does not matter, and it does not matter if the code sets these pointers to 0. (If you are not working with the garbage collector, what you are not doing). The only advantage of setting these pointers to 0 is that your program uses 0 as a flag, which does not mean any object. This design decision and its relevance cannot be answered from the information presented here.

0
source

Source: https://habr.com/ru/post/923276/


All Articles