GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Tue Sep 23, 2014 12:20 am

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: strange error
PostPosted: Thu Apr 10, 2008 1:55 pm 
Offline
Familiar Face

Joined: Thu Apr 10, 2008 1:43 pm
Posts: 19
I just starting with GTK+ but I have some time experience with c and c++.

I'm having a problem executing a simple app I made. Here is the code simplified:

Code:
int main( int   argc, char *argv[] )
{
    GtkWidget *window;
    GtkWidget* vbox;        //Contenedor

    gtk_init (&argc, &argv);

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    build_toolbarMenu( window, vbox );

    GtkWidget* button;
    button =  gtk_button_new_with_label("Hi!");
    gtk_box_pack_start( GTK_BOX(vbox), button, TRUE, TRUE, 3 );

    gtk_widget_show_all( window );
    gtk_main();
}

void build_toolbarMenu( GtkWidget* window, GtkWidget* vbox  )
{
    vbox = gtk_vbox_new( FALSE, 0 );
    gtk_container_add( GTK_CONTAINER(window), vbox );
//    gtk_box_pack_start( GTK_BOX(vbox), SOME_WIDGETS, FALSE, FALSE, 3 );
}


This code execution results in no error compiling, but an error at executing time, just before any windows shows.
Debugging with codeblocks, I got this message:

"Program received signal SIGSEGV, Segmentation fault.
Do you want to view the backtrace?"

If I click on "YES" I see this:

#0 62760DC7 ??() (C:\Archivos de programa\Archivos comunes\GTK\2.0\bin\libgobject-2.0-0.dll:??)
#1 00000000 ??() (??:??)

The line that causes that error is
Code:
gtk_box_pack_start( GTK_BOX(vbox), button, TRUE, TRUE, 3 );

in main function. If I comment this line there is no error.

However if I change the vbox declaration:

Code:
int main( int   argc, char *argv[] )
{
    GtkWidget *window;
    GtkWidget* vbox;        //Contenedor

    gtk_init (&argc, &argv);

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    vbox = gtk_vbox_new( FALSE, 0 );
    gtk_container_add( GTK_CONTAINER(window), vbox );


    //Configuracion ventana
    set_main_window( window );

    //Colocamos el menú
    build_toolbarMenu( window, vbox );

    GtkWidget* button;
    button =  gtk_button_new_with_label("Hi!");
    gtk_box_pack_start( GTK_BOX(vbox), button, TRUE, TRUE, 3 );

    gtk_widget_show_all( window );
    gtk_main();
    return 0;
}


There is no error!
What's wrong with the first example?[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 11, 2008 1:35 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
It comes down to how C passes variables. You will want to do the following:

Code:
int main( int   argc, char *argv[] )
{
    GtkWidget *window;
    GtkWidget* vbox;        //Contenedor

    gtk_init (&argc, &argv);

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    build_toolbarMenu( window, vbox );

    GtkWidget* button;
    button =  gtk_button_new_with_label("Hi!");
    gtk_box_pack_start( GTK_BOX(vbox), button, TRUE, TRUE, 3 );

    gtk_widget_show_all( window );
    gtk_main();
}

void build_toolbarMenu( GtkWidget* window, GtkWidget** vbox  )
{
    &vbox = gtk_vbox_new( FALSE, 0 );
    gtk_container_add( GTK_CONTAINER(window), &vbox );
//    gtk_box_pack_start( GTK_BOX(vbox), SOME_WIDGETS, FALSE, FALSE, 3 );
}


C is passing a copy of the vbox pointer to your function in the original code, so it never actually updates the copy of the pointer in your main function. Therefore, the vbox memory is actually leaked.

The code I just provided passes a pointer to the vbox pointer. When you dereference it, the new GtkVBox widget is actually placed at the correct memory location instead of a copy.

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 14, 2008 7:00 am 
Offline
Familiar Face

Joined: Thu Apr 10, 2008 1:43 pm
Posts: 19
I see but I don't understand why C is passing a copy of pointer.

I thought that the meaning of pointer was exactly to avoid this kind of things.
Passing a variable (not its pointer) passes a copy of that variable, but when I pass a pointer I'm passing the memory address of that variable


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 14, 2008 2:13 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
Think of it this way. When you pass an integer to a function, if you update that integer within the function, it would not be reflected in the variable that was copied in the function call. In C, if you want it to be updated, you would have to pass a pointer to the integer.

With your code, you are passing a pointer to a function. If you update that pointer within the function, it will not be reflected in the variable that was copied in the function call. (Note that GTK+ is updating the pointer address.) In C, if you want it to be updated, you would have to pass a pointer to the pointer.

I realize that I just repeated myself, but it was to show the logic behind how C works with function parameters.

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 14, 2008 3:31 pm 
Offline
Familiar Face

Joined: Thu Apr 10, 2008 1:43 pm
Posts: 19
Now I understand.
gtk_vbox_new() changes the address of the pointer, not the value of the actual address.
Thank you.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group