tags:

views:

133

answers:

2

Hi, I am using glib,
it has a lot of functions that return strings that should be freed myself.

Can I, pass these functions to other functions?

Example: function1 returns a string that must be freed for the caller. function2 returns a pointer to a string that must be freed also.

gchar *string = function2(function1("something"));
g_free (string);

How should I free the string returned from function1? Is this necessary?

Thanks a lot,
and sorry for my english

+4  A: 

Yes it is necessary to free the string returned by function1. To allow you to do this, you need to take a copy of the return value :-

gchar* temp = function1("something");
gchar* string = function2(temp);
g_free(temp);
g_free(string);
Dave Rigby
+1  A: 

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,

Chris Lutz