tags:

views:

48

answers:

2

I am using g_object_set_data to set user name with event_box so in call back i can get it with in event_box pointer.

g_object_set_data(G_OBJECT(event_box), "user_name", (gpointer)(user_name) );

but problem is that i am setting user_name which is not an pointer allocated string.
It is an local string (not allocated on hip) which gets destroyed.

So is it necessary to allocated and then use the pointer, i just want to associate one name with this event_box.

+1  A: 

Yes, since the data stored in the GObject is just a plain pointer, it can't do the memory management for you.

Just call g_strdup() on the string, and store the result.

unwind
but what about freeing it? i am creating this EventBox in loop. Where to delete this allocated string?
PP
+2  A: 

Use the following code:

g_object_set_data_full (G_OBJECT (event_box),
                        "user_name",
                        g_strdup (user_name),
                        (GDestroyNotify) g_free);

This way the string will duplicated on the heap and the copy will get freed automatically when event_box is destroyed.

doublep
Thanks, it worked like a charm! and without any memory leak :)
PP