Will this create a memory leak? Can't determine from top.
- From: Bartek Kostrzewa <bartek runbox com>
- To: gtk-app-devel-list gnome org
- Subject: Will this create a memory leak? Can't determine from top.
- Date: Tue, 28 Jun 2005 11:45:05 +0100
Hi, I'm a starting C/Gtk programmer trying to avoid mistakes. Would the
following create a memory leak or would glib prevent it? (it seems it
does, the RES memory use grows and then stops growing even though I
continue pounding on the buttons)
for(i = 0; i < TABLE_SIZE; i++)
{
for(j = 0; j < TABLE_SIZE; j++)
{
string = g_strdup_printf("Button (%d / %d)", i,j);
button[i][j] = g_object_new(GTK_TYPE_BUTTON, "label", string, NULL);
g_free(string);
gtk_table_attach_defaults(table,
GTK_WIDGET(button[i][j]), i,i+1,j,j+1);
string = g_strdup_printf("Button (%d / %d) clicked.\n", i, j);
/* attach handler to print button which was clicked */
g_signal_connect(button[i][j], "clicked", G_CALLBACK(print_position),
string);
}
}
When I issue a g_free(data) in the callback function and press the same
button twice I get a memory access error (obviously since freeing the
same memory twice is ridiculous)
As I understand it string = g_strdup_printf(... produces a copy and
points string towards that copy, the adress of the copy is then passed
on by g_signal_connect as the data pointer, hence freeing string
destroys the string and should not be done (and doing it produces random
output in the print_button callback)
Should I define an array of char pointers
*message[TABLE_SIZE][TABLE_SIZE] to hold the pointers created by
g_strdup_printf or would that just be a waste of memory?
Thanks! -Bartek
PS: does the mailing list archive feature all e-mail adresses? If so I
should consider using an alias for mailing lists...
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]