fixing memory leaks

Roland Illig roland.illig at gmx.de
Mon Aug 16 10:28:46 UTC 2004


Hi,

I'm currently trying to find and fix some memory leaks. That's rather 
difficult because there are many function taking a "char *" argument 
when a "const char *" would have sufficed. Additionally, it is not 
always clear where the memory should be freed.

To enlighten this situation, I would like to annotate the code with 
small comments that tell the reader about what a function does with its 
arguments. Here they are:

/*in*/          the function does not modify the object
/*free*/        the function will free the object
/*out*/         the function expects an uninitialized object
/*inout*/       the function expects an initialized object and
                 modifies it
/*new*/		the function returns a newly allocated object

declaration example:
/*new*/ char *g_strdup(/*in*/ const char *);

function call example:
char *s = /*new*/ g_strdup("foo");

I know that this will make the code more unreadable, but we could do a 
bug hunting phase where we introduce such comments, then a code cleanup 
phase where we have implicit notations:

1. every const argument is implicitly of type /*in*/.
2. functions generally do not free their arguments, so /*free*/
    keeps on existing.
3. /*out*/ keeps on existing, as it is rarely used.
4. every non-const argument is implicitly of type /*inout*/.
5. /*new*/ keeps on existing, as it should be rarely used.

What are you thinking about this?

Roland



More information about the mc-devel mailing list