3 ms·
> if I see a malloc for a bunch of memory intended for the > lifetime of the app and it's at the beginning of main and > then it doesn't get freed before th
by wahern 9y ago
> if I see a malloc for a bunch of memory intended for the
> lifetime of the app and it's at the beginning of main and
> then it doesn't get freed before the end of process
> execution it's not really a memory leak in my book.
Not freeing such memory and otherwise not explicitly destroying all your state can hide bugs, hide legitimate leaks, and add noise to memory profilers like valgrind. Destroying your state exercises those code pathways, which is very valuable.
Plus, it's just a good habit in a language with manual memory management. It's like wearing your seatbelt--if it's a habit you just do it as a matter of course without thought, no matter whether you're driving around the block or across the country. You start making exceptions, you break the habit, you invite errors of judgment.
- zwieback 9y agoYes, the reasons you mention are why I would always free pre-malloced memory. However, a real memory leak, to me, is memory that gets allocated at different times through the lifetime of the app and doesn't get reliably freed do to unexpected code paths or race conditions. If I don't see the allocation count rise steadily while I run my app I'm not as worried.