Re:Tips and Tricks to avoid memory leaks in writing C
by _delirium on Tuesday June 14, 2011 @07:47PM
(#2655346)
A common issue for people coming from garbage-collected languages is to be very aware of what happens when you malloc() inside a function and return a pointer. Someone is going to have to free it! One common pattern is to have this malloc()ing be restricted to an obviously named foo_init() function, and then pair it with a foo_free() function that gets passed the relevant data structure, and frees anything that the _init() function malloc()ed. You then keep the _init() and _free() functions together and update them in parallel, so all the malloc()s in one are paired with a corresponding free() in the other.
[ Parent
| on HN ]
There are no comments on this comment yet.