In the following code block, I create a pointer to a struct so I can supply multiple variables to a gtk signal handler, which is set to automatically g_free() the struct when the handler is disconnected.
The second part mallocs the variable again and sends the new pointer to the new signal handler. Will this work?
Just from reading it I presume it will leave the first struct data in place at the pointer, and create a new pointer I can change the data in later, all while properly disposing of the memory later.
Knowing that malloc is hardly so simple, I’m wondering if there is something I’m missing, or should take into account.
signaldata * s;
s = (signaldata *) g_malloc(sizeof(signaldata *));
s->col = 0; s->secondaryCol = -1; s->model = GTK_TREE_MODEL(itemModel);
g_signal_connect_data(firstWidget,"edited",(GCallback) treeview_text_edited,s, (GClosureNotify) g_free, 0);
s = (signaldata *) g_malloc(sizeof(signaldata *));
s->col = 1; s->secondaryCol = -1; s->model = GTK_TREE_MODEL(itemModel);
g_signal_connect_data(secondWidget,"edited",(GCallback) treeview_text_edited,s, (GClosureNotify) g_free, 0);
It should be OK because the pointer itself is copied before calling
g_signal_connect_data, so it doesn’t matter that later you assign it a new address by doing a secondg_malloc.However, you lose your only reference to the first
g_malloc‘d memory (unless somehow it becomes accessible fromfirstWidget) and that can be undesirable if you want to do any further manual manipulation to it.