As far as I can see, there is nothing in your implicit destructor that must be called to ensure proper operation. If that is true, you can just forget about cleaning up your lists. Trying to do so will only increase the runtime of your program with absolutely no benefit. Just let the kernel do its job, it won't leak any memory pages just because you couldn't be bothered to clean up static data.
However, if you have a nontrivial destructor down the line somewhere which includes such important operations like flushing files or sending messages to other processes, then you must use a destructor function. I'm not talking about the normal C++ destructors here, but about a specially declared function that is executed by the runtime after main()
exits.
With gcc
, you declare a destructor function like this:
void foo() __attribute__((destructor));
void foo() {
//Do vitally important cleanup here.
}
Since the linker takes care of instructing the runtime to call your destructor function, you do not have to have any call to these functions, they may actually be declared with file local visibility.
Now, you ask "Am I not supposed to delete
this pointer somewhere?" Yes, you are supposed to delete it. You are supposed to call delete
on every object that you create with new
for two reasons:
To give back the memory held by the object to the runtime, so that your process can reuse the memory for other purposes. If you fail to
delete
objects that you create on a regular basis, the memory footprint of your process will increase indefinitely until the kernel steps in and shoots down your process.To run the destructor for your object, which frequently results in calling
delete
on other objects which are not needed anymore. In most cases, this will just give back more memory according to 1., which seems to be your case. It may do more vital operations, though.
Since the objects in question have to live until the very end of your process lifetime (they are static data, after all), you cannot possibly reuse their memory. The kernel, however, is above the level of the runtime that provides you with the new
and delete
keywords. The kernel is the creator of your tiny process world, in which the new
and delete
keywords happen to live. The kernel does not care about which parts of the virtual address space your runtime considers used/unused. The kernel will simply strip down the entire virtual address space when your process exits, and the used/unused state of your memory will dissipate into nothingness.