I do not agree with the votes on this issue. I think this is a real question and rather deep.
On the surface, the answer is "calling free
on any memory that you malloc
ed".
But the real answer is that your design should include a clear ownership model. The only way to avoid memory leaks and access to tattered memory is to always know for each part of the dynamically allocated memory which object owns this memory (and is responsible for deleting it).
If you do not have such a clear ownership model, you will forever catch memory leaks and errors after use (this also applies to C ++). Using a garbage collector will allow you to plaster these problems through significant processor cycles.
If you have a clear ownership model, these problems usually just disappear: the owner of free
all the memory that it owns when it is deleted.
source share