Search code examples
cruntime-errorgtk3

GTK3 button signal errors


I am triyng to make a simple GTK3 window in the C language to show a error message when something happens in the main program, to show to the user what error happened. But, when i try to connect a gtk_main_quit function to the button, the GTK process throws these errors:

GLib-GObject-CRITICAL **: 16:04:36.787: instance with invalid (NULL) class pointer

GLib-GObject-CRITICAL **: 16:04:36.795: g_signal_connect_data: assertion 'G_TYPE_CHECK_INSTANCE (instance)' failed

And then i am stumped to how solve this issue. Maybe I am not initializing correctly the menu?

The way i structered it is by a global struct, that is called and allocated in separated functions, that throws me no errors.

Here is the full code of the function

int mensagem_erro()
 {
    GtkBuilder *construtor = NULL;

    printf("Alocando a janela...\n");
    construtor = malloc(sizeof construtor);

    alojar_erro();

    printf("Gerando a mensagem de erro...\n");
    snprintf(modelo_estrutura_erro->string_erro, TAMANHO_STRING_ERRO-1,"Erro! Erro n %i, 
    do tipo: %s", errno, strerror(errno));

    printf("Criando a tela de erro...\n");
    construtor = gtk_builder_new_from_file("tela_erro.glade");

    if(construtor==NULL)
    {
        printf("Erro! Erro n %i, do tipo: %s", errno, strerror(errno));
        return FALSE;
    }

    printf("Alocando os dados da tela de erro...\n");
    modelo_estrutura_erro->label_erro = GTK_LABEL(gtk_builder_get_object(construtor, 
    "label_erro"));

    if(modelo_estrutura_erro->label_erro==NULL){printf("Erro ao alocar a 
    estrutura!\n");return FALSE;}

    criar_widget(modelo_estrutura_erro->janela_erro, construtor,"janela_erro");
    criar_widget(modelo_estrutura_erro->box_erro, construtor,"box_erro");
    criar_widget(modelo_estrutura_erro->botao_erro, construtor,"botao_erro");
    printf("Inicializando os sinais da tela de erro...\n");
    g_signal_connect(GTK_WIDGET(modelo_estrutura_erro->botao_erro), "clicked", G_CALLBACK(gtk_main_quit),NULL);

    printf("Alocando a mensagem na tela de erro...\n");
    gtk_label_set_text(modelo_estrutura_erro->label_erro, modelo_estrutura_erro-                
    >string_erro);

    printf("Mostrando a janela de erro...\n");
    gtk_widget_show(modelo_estrutura_erro->janela_erro);

    g_object_unref(construtor);
    return TRUE;
}


Solution

  • This is wrong:

    construtor = malloc(sizeof construtor);
    
    1. You need to reserve space with sizeof *constructor not with sizeof constructor, otherwise you reserve space for a pointer.
    2. You replace the space reserved by malloc in this line (memory leak):
    construtor = gtk_builder_new_from_file("tela_erro.glade");
    

    Remove the malloc line (related: when using GTK/GLIB always prefer the safe version g_malloc)

    Regarding your problem:

    g_signal_connect(GTK_WIDGET(modelo_estrutura_erro->botao_erro), "clicked", G_CALLBACK(gtk_main_quit),NULL);
    

    Connect the "clicked" event with a valid instance, clicked wants an object (not a generic widget) as parent.

    Replace GTK_WIDGET(modelo_estrutura_erro->botao_erro) with G_OBJECT(modelo_estrutura_erro->botao_erro).