GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

strange error

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming
Author Message
clinisbut
Familiar Face


Joined: 10 Apr 2008
Posts: 19

PostPosted: Thu Apr 10, 2008 1:55 pm    Post subject: strange error Reply with quote

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: (C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

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: (C)
1
2
3

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: (C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

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]
Back to top
openldev
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 376
Location: State College, Pennsylvania

PostPosted: Fri Apr 11, 2008 1:35 pm    Post subject: Reply with quote

It comes down to how C passes variables. You will want to do the following:

Code: (Plaintext)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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.
Back to top
clinisbut
Familiar Face


Joined: 10 Apr 2008
Posts: 19

PostPosted: Mon Apr 14, 2008 7:00 am    Post subject: Reply with quote

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
Back to top
openldev
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 376
Location: State College, Pennsylvania

PostPosted: Mon Apr 14, 2008 2:13 pm    Post subject: Reply with quote

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.
Back to top
clinisbut
Familiar Face


Joined: 10 Apr 2008
Posts: 19

PostPosted: Mon Apr 14, 2008 3:31 pm    Post subject: Reply with quote

Now I understand.
gtk_vbox_new() changes the address of the pointer, not the value of the actual address.
Thank you.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP