结构、字符串和 gtkwidget 的问题

Probleme with structure, string anf gtkwidget

我正在尝试使用 gtk 在 c 中创建一个立体透视图。我有一个按钮 "previous",它显示上一张图片。所以我将我的按钮连接到一个函数并将它传递给一个结构。但是当我尝试打印我的数组的一个元素时,我有奇怪的字符,我不知道为什么。另外,我的 gtk 图像收到此警告:

GLib-GObject-WARNING **: 19:12:16.442: invalid uninstantiatable type '(null)' in cast to 'GtkImage'
Gtk-CRITICAL **: 19:12:16.442: IA__gtk_image_set_from_file: assertion 'GTK_IS_IMAGE (image)' failed

这是我的结构:

struct ButtonsArg {
    GtkWidget *image;
    char *img[];
};

这是我的主要结构初始化代码:

GtkWidget *image = gtk_image_new();

char *images[nbImages];
//I get the name of all my image in this function
getImageList(images);

//This print work fine
printf("%s\n", images[0]);


struct ButtonsArg * arg;

arg = malloc(sizeof(struct ButtonsArg) + nbImages*sizeof(char*));

for(int i = 0; i < nbImages; i++) {
    arg->img[i] = malloc(strlen(images[i])+1);
    strcpy(arg->img[i], images[i]);
}

arg->image = image;

g_signal_connect(precedent, "clicked", G_CALLBACK(event_precedent), &arg);

这是出现问题的函数:

static void event_previous(GtkWidget *widget, gpointer data) {
    g_print ("previous\n");
    struct ButtonsArg *arg = data;

    //Print weird charac
    for(int i = 0; i < nbImages; i++) {
        printf("%s\n", arg->img[i]);
    }

    GtkWidget *image = arg->image;

    if(currentImage == 0) {
        currentImage = nbImages - 1;
         gtk_image_set_from_file (GTK_IMAGE (arg->image), arg->img[1]);
    } else {
        currentImage--;
        gtk_image_set_from_file (GTK_IMAGE (arg->image), arg->img[2]);
    }
}

如果您有任何建议、帮助或link可能有帮助,感谢您与我分享。

您能否展示您尝试更改 GtkImage 变量的代码部分?似乎程序试图将空值设置为 GtkImage 类型的变量。在这种情况下,任何带有此变量的表达式(如图像->值)都可能引发分割错误。

更新:好的。我看到了。您应该检查 arg-structure。我想,这个结构的某些属性可以有空值。