August 11, 2011

Giorgos Tsiapaliokas Giorgos Tsiapaliokas
Lab Rat
67 posts

[solved] memory deallocation and the delete keyword

Page  
1

Hello,

in some projects i have seen this way of deleting a pointer

  1. MyClass *pointer;
  2. delete pointer;
  3. pointer=0;

why they do this?

After the usage of the delete keyword there is no memory left why they give an invalid memory to the pointer?

Instead of doing

  1. delete pointer;

if i do

  1. pointer = 0;

wiil i have the same result?

thanks in advance

 Signature 

terietor.gr

24 replies

August 11, 2011

rokemoon rokemoon
Lab Rat
197 posts

They do this cuz after

  1. delete pointer;
pointer points to the trash, so if you want to check the poiner in if section you need to set pointer to 0.
  1. if (pointer) {
  2.  //ok there we have alocated object
  3. } esle {
  4.  //there we haven't alocated object
  5.  //and can alocate there if need
  6. }


Instead of doing

delete pointer;
if i do

pointer = 0;


If you set pointer to 0 you’ve get memory leak, cuz you lost pointer to the allocated memory.

August 11, 2011

mlong mlong
Robot Herder
1551 posts

No. It won’t be the same result at all. A pointer simply holds a memory address which points to the start of an area of memory. It isn’t the same as the object contained at that address, itself. Calling delete on a pointer frees the memory at that address, but the pointer still contains a (now-invalid) address. Setting the pointer back to 0 makes it obvious that the pointer is null.

If an invalid pointer is dereferenced, there is undefined behavior as to what will happen. Sometimes things might silently continue to work, sometimes it fails in the most peculiar (or spectacular) ways. However, if a null pointer is dereferenced, the system will typically terminate with a Segmentation Fault (or some comparable memory error) which is pretty easy to track down.

In short, setting the pointer back to 0 can make debugging a lot easier down the line. (But you need to delete the object contained there first!)

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

August 11, 2011

loladiro loladiro
Lab Rat
596 posts

NO!!

  1. delete pointer

actually calls the destructor and releases the memory.
Setting it equal to 0, would let the pointer point to the object at address 0 (your object will never be at address 0). If you don’t call delete, the object will still exist in memory, but you can’t access it anymore. Thus, you created a memory leak. Setting it equal to 0 after deleting is useful for checking whether the object exists. E.g.

  1. if(someCondition)
  2. {
  3. delete pointer;
  4. pointer = 0;
  5. }
  6.  
  7. //and later
  8. if(pointer)
  9. {
  10. //the object still exists, i.e. you can safely use it
  11. }else
  12. {
  13. //it doesn't, do something else
  14. }

August 11, 2011

Volker Volker
Ant Farmer
5428 posts

No, just setting the pointer to 0 does not delete the object.

That’s a good thing™!

Assume the following code:

  1. MyClass *pointer = new MyClass;
  2.  
  3. MyClass *alias = pointer;
  4. alias = 0;
  5.  
  6. pointer->doSomething();

The call to doSomething would fail, if the object had been deleted by assigning 0 to alias.

Resetting the pointer to 0 after a delete can be a good practice in case the pointer can be accessed afterwards. If it had its previous value, using the pointer would lead to a segmentation fault, because the old object is no longer valid. To avoid a null pointer exception, you should check the pointer for being no-null of course :-)

August 11, 2011

mlong mlong
Robot Herder
1551 posts

Wow… this is definitely a hot-button issue! :-) I love the different details which accent each explanation of the core issue.

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

August 11, 2011

rokemoon rokemoon
Lab Rat
197 posts
mlong wrote:
Wow… this is definitely a hot-button issue! :-) I love the different details which accent each explanation of the core issue.

What explanation most liked? ;-)

August 11, 2011

loladiro loladiro
Lab Rat
596 posts
mlong wrote:
Wow… this is definitely a hot-button issue! :-) I love the different details which accent each explanation of the core issue.

Indeed. When I was done typing there were already two other replies and 20sec later Volker’s came to ;). At least one can say we are an active community. Where else do you get 4 elaborate answers within 10min?

August 11, 2011

Lukas Geyer Lukas Geyer
Lab Rat
2074 posts

In addition, deleting objects that already have been deleted result in an error, whereas deleting a null pointer does not. So setting the pointer to 0 after it has been deleted is good practice.

  1. QObject* object = new QObject;
  2.  
  3. delete object; // OK
  4.  
  5. delete object; // FAIL
  6.  
  7. // ...
  8.  
  9. QObject* object = new QObject;
  10.  
  11. delete object; // OK
  12. object = 0;
  13.  
  14. delete object; // OK

August 11, 2011

rokemoon rokemoon
Lab Rat
197 posts

loladiro wrote:

Indeed. When I was done typing there were already two other replies and 20sec later Volker’s came to ;). At least one can say we are an active community. Where else do you get 4 elaborate answers within 10min?

For best frameworks best community :-)

August 11, 2011

Giorgos Tsiapaliokas Giorgos Tsiapaliokas
Lab Rat
67 posts

thank you for your answers

so if i call delete and then allocate to 0,my pointer will point to an invalid address but i will have a mem leak.Correct?

So,if i call delete in my destructor there is no reason to allocate 0 to my pointer.Is that correct?

 Signature 

terietor.gr

August 11, 2011

loladiro loladiro
Lab Rat
596 posts

Summary:

  1. pointer = 0;

Memory leak;

  1. delete pointer;

No memory leak, but no way of detecting it was deleted (and, as Lukas pointed out, calling delete again will result in an error).

  1. delete pointer;
  2. pointer = 0;

No memory leak, you can check whether it still exists with if(pointer)


So,if i call delete in my destructor there is no reason to allocate 0 to my pointer.Is that correct?

Correct (at least as long the pointer itself is not shared with other objects. Then it gets complicated)

August 11, 2011

rokemoon rokemoon
Lab Rat
197 posts

You will have a leak if you set pointer to 0 and then delete it.

Giorgos Tsiapaliwkas wrote:

So,if i call delete in my destructor there is no reason to allocate 0 to my pointer.Is that correct?

This is correct, cuz after destructor you will not need a pointer at all.

August 11, 2011

mlong mlong
Robot Herder
1551 posts

Rule of thumb:
If you need to delete something you have a pointer to:

  • call delete first
  • then set pointer to 0

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

August 11, 2011

Lukas Geyer Lukas Geyer
Lab Rat
2074 posts

If you create a new object using new a block of memory for this desired object is allocated (which means memory is taken from a large pool and reserved for this object) and the address of this memory block is stored to the pointer variable.

If you delete the object using delete the block of memory is returned to the memory pool and can be taken by another object. You pointer still points to the address of the memory where your object was previously stored. If you now access the object through this pointer your program will crash, as the object does no longer exist at this address in memory. This is why you should assign the address 0 the pointer, which indicates that this pointer does not address a valid block of memory.

You do not call delete within your destructor. If you call delete on an object the destructor for this is called by the delete operation.

rokemoon wrote:
You will have a leak if you set pointer to 0 and then delete it.
Giorgos Tsiapaliwkas wrote:
So,if i call delete in my destructor there is no reason to allocate 0 to my pointer.Is that correct?
This is correct, cuz after destructor you will not need a pointer at all.

No, you never ever call delete on the object which is currently destructed.

August 11, 2011

rokemoon rokemoon
Lab Rat
197 posts

And just for future read this [doc.qt.nokia.com] and this [doc.qt.nokia.com]. This will help you not to worry about manually deletion.

Page  
1

  ‹‹ [Solved] When does an object need to be a pointer      [SOLVED] memory deallocation,is there a nicer way to do that? ››

You must log in to post a reply. Not a member yet? Register here!