![](/img/trans.png)
[英][GTK]Passing a loop counter in g_signal_connect to g_callback
[英]Getting GLib warning on passing struct as arg to G_CALLBACK
我試圖將指向struct的指針作為回調函數的參數傳遞,但出現此錯誤
(gtk:11156): GLib-GObject-WARNING **: 22:34:21.308: invalid cast from 'GtkEntry' to 'GtkApplication'
(gtk:11156): Gtk-CRITICAL **: 22:34:21.308: gtk_application_window_new: assertion 'GTK_IS_APPLICATION (application)' failed
執行此代碼時
Appnfile *argptr = (Appnfile*)data;
GtkWidget *window2 = gtk_application_window_new(GTK_APPLICATION(argptr->app1)); //this line has the error
回調函數的原型是
static void second_win(GtkEntry *entry, gpointer data)
;
聲明struct和g_signal_connect行是
Appnfile arg;
arg.app1=app;
arg.buff=buffer;
g_signal_connect(name, "activate",G_CALLBACK(second_win),&arg);
struct的定義是
typedef struct {
GApplication *app1;
GtkEntryBuffer *buff;
} Appnfile;
鏈接到程序在這里
當我將應用程序作為數據傳遞給g_signal_connect_swapped函數時,代碼工作正常。 但是在傳遞與struct元素相同的變量時,我得到了這個警告。
亞歷山大·德米特里耶夫(Alexander Dmitriev)正確。 您正在嘗試在second_win
使用arg
,但是由於arg
是在堆棧上分配的結構,因此從該函數返回后,該結構便不復存在。 有兩種解決方案arg
壽命足夠長:
malloc
或g_malloc
/ g_new
/ g_new0
) arg
情況下才將其分配到函數中的堆棧上 這是使用第二種解決方案的補丁。 您可以將其保存在arg.patch
,並與git am arg.patch
:
From 6c0679485cde31c55c58aa5f54f0a60d4c874d71 Mon Sep 17 00:00:00 2001
From: Luis Menina <liberforce@freeside.fr>
Date: Wed, 27 Feb 2019 10:41:58 +0100
Subject: [PATCH] Create argument list where it will live long enough to be
accessed
Declaring it on the stack inside a callback will make it be destroyed
when you exit the callback. By declaring it in the stack, in the main
you are sure that the structure has the same lifespan as your
application.
---
prog_c/gtk_pr_a.c | 13 ++++++++-----
1 file changed, 8 insertions(+), 5 deletions(-)
diff --git a/prog_c/gtk_pr_a.c b/prog_c/gtk_pr_a.c
index fb3b5a2..d11ba7d 100644
--- a/prog_c/gtk_pr_a.c
+++ b/prog_c/gtk_pr_a.c
@@ -80,6 +80,7 @@ static void first_win(GApplication *app, gpointer data){
GObject *window, *name;
GError *error =NULL;
GtkEntryBuffer *buffer;
+ Appnfile *arg = data;
builder= gtk_builder_new();
if(gtk_builder_add_from_file( builder, "pr1.ui",&error)==0){
@@ -97,10 +98,9 @@ static void first_win(GApplication *app, gpointer data){
gtk_widget_show_all(GTK_WIDGET(window));
g_signal_connect( name, "activate", G_CALLBACK(search_user),buffer);
- Appnfile arg;
- arg.app1=app;
- arg.buff=buffer;
- g_signal_connect(name, "activate",G_CALLBACK(second_win),&arg);
+ arg->app1=app;
+ arg->buff=buffer;
+ g_signal_connect(name, "activate",G_CALLBACK(second_win), arg);
g_signal_connect_swapped( name, "activate", G_CALLBACK(gtk_window_close),window);
g_object_unref(window);
}
@@ -117,7 +117,10 @@ static void first_win(GApplication *app, gpointer data){
int main(int argc, char *argv[]){
GtkApplication *app = gtk_application_new("org.my.app",
G_APPLICATION_FLAGS_NONE);
- g_signal_connect(app, "activate", G_CALLBACK(first_win),NULL);
+ Appnfile arg;
+ memset(&arg, 0, sizeof(arg));
+
+ g_signal_connect(app, "activate", G_CALLBACK(first_win), &arg);
//g_signal_connect(app, "2win",G_CALLBACK(second_win),NULL);
int status =g_application_run(G_APPLICATION(app),argc, argv);
g_object_unref(app);
--
2.17.1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.