I have a simple program which reads a bunch of ini file settings in memory allocated dynamically (malloc), then does stuff in loops for a long time, then ends. When I run valgrind I see that the memory I malloc’ed for my ini strings is not freed.
On the one hand, I think that it shouldn’t matter since the program is shutting down (and no memory is leaked in the loops).
On the other hand, I like when valgrind gives me a big pat on the back for cleaning up my own mess. Aside from the pat on the back…is it good practice to release every malloc’ed space upon termination (or just let the OS cleanup)? If it is, how can I track which of my pointers point to malloc’ed memory (versus pointing to string constants which are the defaults) to ensure I’m releasing the right stuff?
The main advantage to freeing mallocs at shutdown is to help valgrind track down your memory leaks – you can’t find true memory leaks when you have pages full of false positives, after all. Apart from that, though, there’s no harm in letting the OS clean up.
As for keeping track of string constants vs heap allocated values, one simple policy would be to always use heap values – fill in the defaults with
strdup()d strings at startup.