GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Wed Jul 23, 2014 3:43 am

All times are UTC




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: GtkFileChooser problem
PostPosted: Mon Dec 17, 2012 3:00 am 
Offline
Familiar Face

Joined: Sun Dec 16, 2012 9:09 pm
Posts: 12
Dear there,

I am new to Gtk programming, please bear with me -:)
I have created GUI using Glade, where I have FileChooserButton(s) called filechooserbutton1, filechooserbutton2,and filechooserbutton3.
Following tutorials, I can compile and run, and see the main window. However, each time I click a button, I get segment fault. Gdb shows the following code are suspectful:

Code:
  GtkFileChooser *b[3];
  b[0]= (GtkFileChooser *)data->filechooserbutton1;
  b[1]= (GtkFileChooser *)data->filechooserbutton2;
  b[2]= (GtkFileChooser *)data->filechooserbutton3;

  int i;

  for (i=0;i<3;i++){
    printf("%d: %x\n",i, b[i]);
  }

  for (i=1;i<3;i++){
    file = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(b[i]));
}

As the output is:
0: 7
1: 26d4210
2: 27fa1b0

The question is:
(1) Why didn't the filechooserbutton1 get a correct address?
(2) gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(b[1])) caused segmentation. But it has an address!


Top
 Profile  
 
 Post subject: Re: GtkFileChooser problem
PostPosted: Mon Dec 17, 2012 11:26 am 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
hmmm. Can't really tell too well. Could you give more background information (version of gtk used, your OS, link to the particular tutorial where code is contained for the main part and how your code may vary from it)
Could it be that you don't have a filename associated with those widgets yet?
Personally I'm not too keen on the way the file chooser array is set up as multiple names for objects can make things go wrong quite easily. I think it would be cleaner to have an array of buttons in your data structure and accessing them such as (GtkFileChooser*)data->button[j] rather than naming them in such a way that makes it more difficult to iterate over.
Also if you are using gdb for debugging it works better to set break points in your code rather than inserting code to print debug information. That way you can query things a lot more thoroughly.
incidently you don't need to cast in the last loop as the type already matches. (oh and glib has some nice replacements for printf and its friends that can avoid some of its common pitfalls)


Top
 Profile  
 
 Post subject: Re: GtkFileChooser problem
PostPosted: Mon Dec 17, 2012 8:42 pm 
Offline
Familiar Face

Joined: Sun Dec 16, 2012 9:09 pm
Posts: 12
thanks, Paul, while I am trying to clean up my code to upload here. I want to say that I particularly agree with you that associate three buttons with:
filechooserbutton1
filechooserbutton2
filechooserbutton3

is both inefficient and error-prone. Unfortunately, I am relying on Glade3 to generate "most" GUI code as I am not that familiar with API yet. Glade3 does not seem to have a way for me to generate an array of file chooser buttons. What a pity it is!


Top
 Profile  
 
 Post subject: Re: GtkFileChooser problem
PostPosted: Tue Dec 18, 2012 2:01 am 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
If you already have them established you can just add them to a glib data type quite easily (see http://developer.gnome.org/glib/2.34/glib-data-types.html). Here's an example for initialising a GArray with the buttons (I never learnt Glade so have the commands to set up the buttons manually):
Code:
butt_array=g_array_sized_new(FALSE, FLASE, sizeof(GtkWidget), 3);
butt=gtk_file_chooser_button_new("Select a file", GTK_FILE_CHOOSER_ACTION_OPEN);
gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(butt), "~/.appname/settings.conf");
gtk_grid_attach(grid, butt, 0, 0, 1, 1);
gtk_widget_show(butt);
g_array_append_val(butt_array, butt);
butt=gtk_file...
...
g_array_append_val(butt_array, butt);


Top
 Profile  
 
 Post subject: Re: GtkFileChooser problem
PostPosted: Tue Dec 18, 2012 5:23 pm 
Offline
Familiar Face

Joined: Sun Dec 16, 2012 9:09 pm
Posts: 12
Dear Paul,

So I used GDB to debug my code, and here is the output. The structure "data" holds points to the widgets after building the GUI. As shown the data->filechooserbutton0 pointer is: (GtkWidget *) 0x7e5320 after this step. I pass "data" to callback function gtk2me_import_name(data), and do further processing. What surprised me is that data->filechooserbutton0 changed its value to (GtkWidget *) 0xa6c4d0 in this callback function! No wonder the program crashed in a later step where I try to access the filename from the data->filechooserbutton0. How could data->filechooserbutton0 change its value?


Code:
Breakpoint 1, main (argc=1, argv=0x7fffffffe368) at main.c:39
(gdb) p data->filechooserbutton0
$1 = (GtkWidget *) 0x0
(gdb) n
[Thread 0x7fffea500700 (LWP 15601) exited]
[Thread 0x7fffdb7ae700 (LWP 15606) exited]
[Thread 0x7fffe9caf700 (LWP 15602) exited]
[Thread 0x7fffdbfff700 (LWP 15605) exited]
(gdb) p data->filechooserbutton0
$2 = (GtkWidget *) 0x7e5320
(gdb) p data->files[0]
$3 = (GtkWidget *) 0x7e5320
(gdb) n
[Thread 0x7fffdaf5d700 (LWP 15607) exited]
(gdb)
(gdb) p data->files[1]
$4 = (GtkWidget *) 0x7e5000
(gdb) p data->files[2]
$5 = (GtkWidget *) 0x7e5190
(gdb) p data->filechooserbutton1
$6 = (GtkWidget *) 0x7e5000
(gdb) p data->filechooserbutton2
$7 = (GtkWidget *) 0x7e5190
(gdb) b gtk2me_import_name
Breakpoint 2 at 0x412e0f: file callbacks.c, line 73.
(gdb) c
Continuing.
[New Thread 0x7fffdaf5d700 (LWP 15610)]
[New Thread 0x7fffdbfff700 (LWP 15611)]
[New Thread 0x7fffe9caf700 (LWP 15612)]
[New Thread 0x7fffdb7ae700 (LWP 15613)]
[Thread 0x7fffdaf5d700 (LWP 15610) exited]
[Thread 0x7fffdb7ae700 (LWP 15613) exited]
[Thread 0x7fffe9caf700 (LWP 15612) exited]
[New Thread 0x7fffe9caf700 (LWP 15615)]
[New Thread 0x7fffdb7ae700 (LWP 15616)]
[New Thread 0x7fffdaf5d700 (LWP 15617)]
[New Thread 0x7fffda177700 (LWP 15618)]
[New Thread 0x7fffd9926700 (LWP 15619)]
[New Thread 0x7fffd90d5700 (LWP 15620)]
[New Thread 0x7fffd8884700 (LWP 15621)]
[New Thread 0x7fffbffff700 (LWP 15622)]
[New Thread 0x7fffbf7ae700 (LWP 15623)]
[Thread 0x7fffda177700 (LWP 15618) exited]
[Thread 0x7fffd8884700 (LWP 15621) exited]
[Thread 0x7fffd90d5700 (LWP 15620) exited]
[Thread 0x7fffbf7ae700 (LWP 15623) exited]
[Thread 0x7fffdbfff700 (LWP 15611) exited]
[Thread 0x7fffdb7ae700 (LWP 15616) exited]
[Thread 0x7fffdaf5d700 (LWP 15617) exited]
[Thread 0x7fffbffff700 (LWP 15622) exited]
[Thread 0x7fffd9926700 (LWP 15619) exited]
[New Thread 0x7fffd9926700 (LWP 15624)]
[Thread 0x7fffe9caf700 (LWP 15615) exited]

Breakpoint 2, gtk2me_import_name (data=0xa531b0) at callbacks.c:73
(gdb) p data->filechooserbutton1
$8 = (GtkWidget *) 0x7ed1a0
(gdb) p data->filechooserbutton0
$9 = (GtkWidget *) 0xa6c4d0
(gdb) n
(gdb)
(gdb)
(gdb)
(gdb)
[Thread 0x7fffd9926700 (LWP 15624) exited]

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff723fdc0 in g_type_check_instance_cast () from /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0


Top
 Profile  
 
 Post subject: Re: GtkFileChooser problem
PostPosted: Tue Dec 18, 2012 9:37 pm 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
Not too sure, that's more a question for your callback. Could there have been any ownership of the objects transferred? In such a case you need to bracket such callbacks with g_object_ref and g_object_unref. Is it your own callback or do you have access to debug symbols for it?


Top
 Profile  
 
 Post subject: Re: GtkFileChooser problem
PostPosted: Wed Dec 19, 2012 3:58 pm 
Offline
Familiar Face

Joined: Sun Dec 16, 2012 9:09 pm
Posts: 12
Thanks for everyone's reply. It turned out that I got the prototype of the callback function wrong. Hence the pointers are all meaningless.


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

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 1 guest


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