12

I have a small program where a gtk signal callback function needs 2 or 3 variables.

I don't want to make these global variables (The entire goal of the project is to be neat and tidy) and I don't want to make a whole struct just so I can send a widget and a compiled regex to a function.

As far as I've seen g_signal_connect only allows for a single data variable.

Would the most efficient way of doing this perhaps be an array of void pointers to the two objects in question? Something like this?

void * data[2];
data[0] = widget;
data[1] = compiledregex;
g_signal_connect(save,"clicked",G_CALLBACK(callbackfunction),data);

3 Answers 3

11

Of course you can use arrays of void pointers, but if you want to pass around values with different types (especially values whose type is longer than sizeof(void *)), you can't use arrays. For these, you'll almost certainly want to wrap them in a struct and pass the struct's address as the data parameter.

Example:

struct my_struct *data = malloc(sizeof(*data));
data->field_one = value_one;
data->field_two = value_two; /* etc. */

g_signal_connect(save, "clicked", callback, data);

Of course, don't forget to free(data) in the callback function (assuming it's for single use).

Edit: as you wanted an example with void **, here you are (this is ugly, and I don't recommend you to use this -- either because allocating a one-element array for primitive types is wasting your shoot or because casting a non-pointer to void * is bad practice...):

void **data = malloc(sizeof(data[0]) * n_elements);

type1 *element1_ptr = malloc(sizeof(first_item));
*element1_ptr = first_item;
data[0] = element1_ptr;

/* etc. */

To free them:

int i;
for (i = 0; i < n_elements; i++)
    free(data[i]);

free(data);
16
  • But aren't pointers always the same size? Can't I just pass pointers to the variables in the array and dereference them inside the callback function?
    – J V
    Commented Aug 4, 2012 at 13:49
  • @JV in practice, pointers are of the same size, so sizeof(void *) == sizeof(char *) == sizeof(whatever *), but this is not guaranteed by C.
    – user529758
    Commented Aug 4, 2012 at 13:52
  • 2
    @JV that's called object-oriented programming design :) (and maybe a hash table)
    – user529758
    Commented Aug 4, 2012 at 14:16
  • 2
    @JV I just explained it to you. Automatic variables are deallocated when the function which they've been created in returns.
    – user529758
    Commented Aug 4, 2012 at 14:33
  • 1
    No, it isn't. How you would you do that?
    – user529758
    Commented Aug 4, 2012 at 14:42
7

You can use g_object_set_data() and g_object_get_data(). First, set the data:

g_object_set_data(G_OBJECT(my_edit), "my_label", my_label);
g_object_set_data(G_OBJECT(my_edit), "age", GINT_TO_POINTER(age));

and in the callback you can get the data like so:

gint age = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "age"));
GtkWidget *my_label = g_object_get_data(G_OBJECT(widget), "my_label");
1
  • 1
    Don't forget to free the data before destroying the object.
    – ptomato
    Commented Aug 8, 2012 at 21:23
3

Expanding on H2CO3's answer, you can also set the data (and I strongly recommend using a struct, by the way) to be freed automatically when the signal handler is disconnected:

g_signal_connect_data(save, "clicked", callback, data, (GClosureNotify)free, 0);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.