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 

segmentation fault on gtk_widget_destroy

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


Joined: 29 Feb 2008
Posts: 7

PostPosted: Sat Apr 26, 2008 2:09 am    Post subject: segmentation fault on gtk_widget_destroy Reply with quote

Hi,

My GTK application is involved with creating a table of x rows and y columns. This table added to the toplevel window as

gtk_container_add (GTK_CONTAINER (window), table);
gtk_widget_show_all (window);

My "update" function retrieves data and controls display from above table. At some later time during the update, I want to have a total different display which I need to destroy current table and create a new table of different size of rows and columns.

gtk_widget_destroy (GTK_WIDGET (table));
table = gtk_table_new (newrow, newcol, FALSE);

I got a segmentation fault at the "gtk_widget_destroy" call. How do I resolve this situation ? Please help.

ctran
Back to top
openldev
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 386
Location: Fairfax, Virginia

PostPosted: Sat Apr 26, 2008 4:44 am    Post subject: Reply with quote

Could you post a little bit more code? We can't see what's going on in the update function ...
Back to top
ctran
Familiar Face


Joined: 29 Feb 2008
Posts: 7

PostPosted: Sat Apr 26, 2008 12:16 pm    Post subject: Reply with quote

Right now I used time to change display page but later will link to shared memory where I rely on some variable change to change this display page.

The segmentation fault occurred when changing to special page.

gint
update(gpointer data)
{
...
gettimeofday(&t_now, NULL);
deltat = (t_now.tv_sec - t_prev.tv_sec)*1000 +
(t_now.tv_usec - t_prev.tv_usec + 500)/1000;

if (deltat > 15000 && !set_special_page) {
create_special_page();
set_special_page = 1;
special_page = 1;
}

if (deltat > 30000 && !set_regular_page) {
create_regular_page();
set_regular_page = 1;
special_page = 0;
}
...
}

void
create_regular_page()
{
if(table) {
gtk_widget_destroy(GTK_WIDGET(window), table);
table = gtk_table_new(16, 3, FALSE);
....
}
}

void
create_special_page()
{
if(table) {
gtk_widget_destroy(GTK_WIDGET(window), table);
table = gtk_table_new(33, 8, FALSE);
....
}
}

int
main (int argc,
char *argv[])
{
...

create_regular_page();

gtk_container_add (GTK_CONTAINER (window), table);
gtk_widget_show_all (window);

gettimeofday(&t_prev, NULL);
timer = g_timeout_add(1000, update, hdr_ebox);

gtk_main ();
return 0;
}
Back to top
openldev
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 386
Location: Fairfax, Virginia

PostPosted: Sat Apr 26, 2008 2:59 pm    Post subject: Reply with quote

First, gtk_widget_destroy() takes a single parameter. You are destroying your window and then recreating the table. Second, how are you passing the table to the function? You might want to read this post: http://www.gtkforums.com/viewtopic.php?p=2933
Back to top
ctran
Familiar Face


Joined: 29 Feb 2008
Posts: 7

PostPosted: Sat Apr 26, 2008 3:21 pm    Post subject: Reply with quote

Sorry, I typed it wrong when I posted because I had this in my code

gtk_widget_destroy (GTK_WIDGET (top_table));

Just simply rename as table instead of top_table.

Everything else is the same.

My window, table variables are declared as global, above all functions and main.
Back to top
openldev
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 386
Location: Fairfax, Virginia

PostPosted: Sat Apr 26, 2008 4:17 pm    Post subject: Reply with quote

When in the code is this table every created initially? The first time you call gtk_widget_destroy(), it is on an invalid memory location.
Back to top
ctran
Familiar Face


Joined: 29 Feb 2008
Posts: 7

PostPosted: Sat Apr 26, 2008 4:36 pm    Post subject: Reply with quote

The call to create_regular_page in main created the table the first time. Since this table variable is defined globally, it should be defined in create_special_page. I put down some printf statements and the regular page worked fine and after 15 secs I got seg fault when calling special page. It failed at gtk_widget_destroy.

You think I should define variables locally and pass them to functions ?
Back to top
ctran
Familiar Face


Joined: 29 Feb 2008
Posts: 7

PostPosted: Sun Apr 27, 2008 12:31 am    Post subject: Reply with quote

One thing I have for the regular page that I connect a signal to an expose event to do some drawing (line, arrow, etc.) while updating. So by destroying the table like this it caused some problem. I will look again in my codes.

I wrote a much simpler example with 5x1 table with label widgets created first time and later destroy it and recreated with 2x1 table and it worked fine.

If I need to handle anything special for event prior to destroying the table, please let me know. Thanks for your help.
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