I'm trying to pass multiple arguments to a gtk call back function I have the following code so far:
我试图将多个参数传递给gtk回调函数我到目前为止有以下代码:
void add_new_set(GtkDialog *dialog, gint response_id, gpointer callback_params)
{
g_print (gtk_entry_get_text (((struct data *) callback_params)->entry));
}
struct data callback_params;
callback_params.entry = gtk_entry_new();
gtk_container_add(GTK_CONTAINER(content_area), callback_params.entry);
g_signal_connect(dialog,"response",G_CALLBACK (add_new_set),&callback_params);
nothing get g_print ed I get teh following error instead: (tat:5918): Gtk-CRITICAL **: IA__gtk_entry_get_text: assertion 'GTK_IS_ENTRY (entry)' failed
没有得到g_print ed我得到以下错误:(tat:5918):Gtk-CRITICAL **:IA__gtk_entry_get_text:断言'GTK_IS_ENTRY(条目)'失败
(tat:5918): GLib-CRITICAL **: g_print: assertion 'format != NULL' failed
(tat:5918):GLib-CRITICAL **:g_print:断言'格式!= NULL'失败
I'm open to using techniques other than passing a struct pointer
我愿意使用除传递结构指针之外的技术
thanks
谢谢
1 个解决方案
#1
2
You practically need to pack the composite data in a heap-allocated struct
, and pass the pointer to the callback.
实际上,您需要将复合数据打包在堆分配的结构中,并将指针传递给回调。
struct data *cb_data = g_new0(struct data, 1);
cb_data->entry = gtk_entry_new();
cb_data->foo = "somefoo";
g_signal_connect(dialog,"response",G_CALLBACK (add_new_set), cb_data);
But you might instead have a single static
variable of some struct
type, and pass the address of that variable to your callback. This is usually bad practice and I don't recommend coding like this (since you want a callback to be somehow reentrant).
但是你可能只有一个结构类型的静态变量,并将该变量的地址传递给你的回调。这通常是不好的做法,我不建议像这样编码(因为你希望回调以某种方式重入)。
You cannot take the address of a local variable and pass it to g_signal_connect
(because GTK signal handling will use that pointer long after you have returned from your function so popped its call frame).
您不能获取局部变量的地址并将其传递给g_signal_connect(因为GTK信号处理将在您从函数返回后很长时间使用该指针,因此弹出其调用帧)。
Of course, the issue is when should your program free that cb_data
. Perhaps consider g_signal_connect_data which has a destroy_data
closure notification. Or connect another signal to free that data (perhaps widget "destroy" or dialog "close" on your dialog
....).
当然,问题是你的程序什么时候应该释放cb_data。也许考虑具有destroy_data闭包通知的g_signal_connect_data。或者连接另一个信号以释放该数据(可能是对话框中的“销毁”或对话框“关闭”....)。
You should consider using valgrind to debug memory leaks.
您应该考虑使用valgrind来调试内存泄漏。
#1
2
You practically need to pack the composite data in a heap-allocated struct
, and pass the pointer to the callback.
实际上,您需要将复合数据打包在堆分配的结构中,并将指针传递给回调。
struct data *cb_data = g_new0(struct data, 1);
cb_data->entry = gtk_entry_new();
cb_data->foo = "somefoo";
g_signal_connect(dialog,"response",G_CALLBACK (add_new_set), cb_data);
But you might instead have a single static
variable of some struct
type, and pass the address of that variable to your callback. This is usually bad practice and I don't recommend coding like this (since you want a callback to be somehow reentrant).
但是你可能只有一个结构类型的静态变量,并将该变量的地址传递给你的回调。这通常是不好的做法,我不建议像这样编码(因为你希望回调以某种方式重入)。
You cannot take the address of a local variable and pass it to g_signal_connect
(because GTK signal handling will use that pointer long after you have returned from your function so popped its call frame).
您不能获取局部变量的地址并将其传递给g_signal_connect(因为GTK信号处理将在您从函数返回后很长时间使用该指针,因此弹出其调用帧)。
Of course, the issue is when should your program free that cb_data
. Perhaps consider g_signal_connect_data which has a destroy_data
closure notification. Or connect another signal to free that data (perhaps widget "destroy" or dialog "close" on your dialog
....).
当然,问题是你的程序什么时候应该释放cb_data。也许考虑具有destroy_data闭包通知的g_signal_connect_data。或者连接另一个信号以释放该数据(可能是对话框中的“销毁”或对话框“关闭”....)。
You should consider using valgrind to debug memory leaks.
您应该考虑使用valgrind来调试内存泄漏。