Re: whos to free what mem?

On Wed, 2004-05-05 at 11:31 +0200, Andreas Leitner wrote:

I am developing GTK bindings for the language Eiffel and am currently
investigating who needs to free what memory with respect to strings.

In the following scenario:
GtkWidget *button;
char* label_text = ...; // somehow init that text
printf ("label text: %x\n", label_text);
button = gtk_button_new_with_label (label_text);
printf ("get_text: %x\n", gtk_button_get_label (GTK_BUTTON(button)));
I can see that gtk_button_new_with_label actually copies the string
since the first and the second printf show two different memory

* Now am I correct when assuming that if I have dynamically allocated
the memory for 'label_text', it is my duty to free that memory again? 

Yes. blah_set_X() will always copy the given property value into the
object. The arguments to blah_new() merely set those properties before
you get the object.

* Is this true for all the string handling in GTK? Are strings always
copied, and I always need to free them? Or are there exceptions? If so
is there documentation who needs to free what where and when? (;

The API docs *usually* mention when you have to free and when you don't
(e.g. "you must free the returned X when no longer needed"). Further,
the functions return const when you shouldn't mess with the result, and
have const arguments when the object won't mess with the passed values.
The only exception to this rule I'm aware of is char** arrays.

many thanks in advance,


    Jim Cape

    "We still name our military helicopter gunships after victims
     of genocide. Nobody bats an eyelash about that: Blackhawk.
     Apache. And Comanche. If the Luftwaffe named its military
     helicopters Jew and Gypsy, I suppose people would notice."
       -- Noam Chomsky, "Propaganda and the Public Mind"

Attachment: signature.asc
Description: This is a digitally signed message part

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]