Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ should i bother deleting pointers to application lifetime variables?

Tags:

c++

memory

I have a few "global" constructs that are allocated with new and are alive the entirety of the applications life span.

Should i bother calling delete on the pointers just before the application finishes? Doesn't all the of the applications memory get reclaimed after it closes anyway?

Edit For Clarity. I am only talking about not calling delete for lifetime objects who "die" right as the program is closing.

like image 352
user936509 Avatar asked Dec 21 '11 10:12

user936509


People also ask

Should you always delete a pointer?

Pointers are just memory addresses. They can point to anything: A variable on the stack, memory created on the heap using new , memory created on the heap using the old C malloc function… Pointers to variables on the stack do not need to be deleted.

Do you need to set Nullptr after delete?

Delete operation performs a null pointer check anyway. So performing another null pointer check is an unnecessary operation and for me personally it makes the code ugly. But of course it is a good practice to set the pointer to NULL/nullptr after deleting as the pointer location is invalid after deletion.

What does deleting a pointer do?

The pointer will point to no object afterwards if you only set it to null, but the memory of the object it pointed to previously won't be freed. So in effect you will leak memory by doing that.

What happens if a program attempts to delete Nullptr?

What happens when delete is used for a NULL pointer? Explanation: Deleting a null pointer has no effect, so it is not necessary to check for a null pointer before calling delete.


2 Answers

Technically, yes, the memory is reclaimed. But unless you use delete the destructors of those objects are not run and their side effect is not applied. This might lead to a temporary file not deleted or a database change not committed depending on what those destructor were meant to do.

Also don't forget Murphy. Now the code for managing those objects is used as you describe (objects have to persist for the life of the program) but later you might want to reuse the code so that it is run multiple times. Unless it can deal with recreating objects properly it will be leaking objects.

like image 118
sharptooth Avatar answered Oct 20 '22 16:10

sharptooth


It is always good practice to clean up everything, although the memory is reclained these objects might have other resources allocated (shared memory, smeaphores etc) that should be cleaned up, probably by the objects destructors.

If you do not want to call delete use shared pointers to hold these resources, so that they are cleaned up correctly when the application exits.

How are you testing your application? Not cleaning up might hinder development of a decent test harness. Tests of the application might want a way of spoofing a shutdown and restart.

There is more to cleaning up that simple releasing memory.

like image 33
mark Avatar answered Oct 20 '22 17:10

mark