In the example you give:
gchar *string = function2(function1("something"));
g_free (string);
You can't free the string from function1()
. This is a memory leak. So while I know it's nice to compress things, you should slow down:
gchar *temp, *string;
temp = function1("something");
string = function2(temp);
g_free(temp);
temp = NULL;
g_free(string);
Otherwise, every time that code runs, function1()
will be allocating more memory that never gets freed, and if your application runs for a long time, your program will start slowly running out of avaliable memory (because of all that allocated memory that is never free()
d).
Another alternative is to write a wrapper around function2()
:
gchar *function2_wrapper(gchar *c)
{
gchar *ret = function2(c);
free(c);
return ret;
}
gchar *string = function2_wrapper(function1("something"));
But in my opinion that's probably more effort than it's worth, and anyone looking at your code may be lost, think there is a memory leak when there isn't, and rewrite it and get hit with a double-free()
error at runtime and have no idea what's going on,