GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Fri Aug 29, 2014 3:13 am

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Segfault when trying to create a new window GtkWidget
PostPosted: Sun Nov 27, 2011 5:46 pm 
Offline

Joined: Sun Nov 27, 2011 5:39 pm
Posts: 2
Hey guys,first of all Hi,I`m a 16 year old beginner C/GTK+ programmer (been using Linux by 3 years now)
It Segfaults when trying to create the "x" window.
I have this problem when trying to create a new window,analyze the code and please point out a solution
Code:
#include <gtk/gtk.h>
//the one to create after the first
GtkWidget *x;
static void create_window(int w,int h) {
   GtkWidget *newlabel;
   x = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   newlabel = gtk_label_new("Salut Pula!");
   gtk_window_set_default_size(GTK_WINDOW(x),w,h);
   gtk_container_set_border_width(GTK_CONTAINER(x),10);
   gtk_container_add(GTK_CONTAINER(x),newlabel);
   gtk_widget_show(newlabel);
   gtk_widget_show(x);
}
static void toggle(GtkWidget *widget,gpointer data) {
   if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget))) {
      create_window(200,200);
   } else {
      gtk_widget_hide(x);
   }
}
static void die(GtkWidget *widget,gpointer data) {
   gtk_main_quit();
}
int main(int argc, char *argv[]) {
   //forgot it again...
   gtk_init(&argc,&argv);
   //window,togglebutton
   GtkWidget *window;
   GtkWidget *toggle;
   window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   g_signal_connect(window,"delete-event",G_CALLBACK(die),NULL);
   gtk_window_set_default_size(GTK_WINDOW(window),300,300);
   toggle = gtk_toggle_button_new_with_label("Apasa!");
   //is the problem with the "toggled" signal or the callback func?
   g_signal_connect(toggle,"toggled",G_CALLBACK(toggle),NULL);
   gtk_container_add(GTK_CONTAINER(window),toggle);
   gtk_widget_show(toggle);
   gtk_widget_show(window);
   gtk_main();
}
   




Top
 Profile  
 
 Post subject: Re: Segfault when trying to create a new window GtkWidget
PostPosted: Sun Nov 27, 2011 6:39 pm 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 745
Location: UK
Hi,

Your segmentation problem is due to you using a local variable "toggle" in the function main(), but also expecting to be able to have access to the function toggle(), which you can not do in C. This was fixed by renaming the local variable.

I have slightly rewritten your code to correct that problem and re-formatted it so that it is easier to read. The main things with reformatting was to move the variable definitions in the function main() to the start of the function so that it will compile under more C compilers. I can make my compiler complain about it if I ask it to warn me about everything and be strict (note this is how I found the bug). I also added more spaces between lines, function arguments, etc., as before it was almost like reading a book with all the spaces taken out!!!!

Note I have left a bug in the application for you ;-) It is a memory leak. You are able to create and hide your second window as many times as you like, and every time you lose the reference to the old window. But you never destroy the window.
Code:
#include <gtk/gtk.h>

//the one to create after the first
GtkWidget *x;

static void create_window(int w, int h)
{
   GtkWidget *newlabel;

   x = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   newlabel = gtk_label_new("Salut Pula!");
   gtk_window_set_default_size(GTK_WINDOW(x), w, h);
   gtk_container_set_border_width(GTK_CONTAINER(x), 10);
   gtk_container_add(GTK_CONTAINER(x), newlabel);

   gtk_widget_show_all(x);
}

static void toggle(GtkWidget *widget, gpointer data)
{
   if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget))) {
      create_window(200, 200);
   } else {
      gtk_widget_hide(x);
   }
}

static void die(GtkWidget *widget, gpointer data)
{
   gtk_main_quit();
}

int main(int argc, char *argv[])
{
   //window,togglebutton
   GtkWidget *window;
   GtkWidget *toggle_button;

   //forgot it again...
   gtk_init(&argc, &argv);

   window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   g_signal_connect(window, "delete-event", G_CALLBACK(die), NULL);
   gtk_window_set_default_size(GTK_WINDOW(window), 300, 300);
   toggle_button = gtk_toggle_button_new_with_label("Apasa!");

   //is the problem with the "toggled" signal or the callback func?
   g_signal_connect(toggle_button, "toggled", G_CALLBACK(toggle), NULL);
   gtk_container_add(GTK_CONTAINER(window), toggle_button);

   gtk_widget_show_all(window);
   gtk_main();

   return 0;
}

_________________
E.


Top
 Profile  
 
 Post subject: Re: Segfault when trying to create a new window GtkWidget
PostPosted: Mon Nov 28, 2011 3:34 am 
Offline

Joined: Sun Nov 27, 2011 5:39 pm
Posts: 2
Thanks very much Errol,I forgot i couldn`t use same identifiers for a function and a variable.

Also what i realized that i would hide "x" but then i would create another window,and hiding that,and creating yet another window,which started taking up space.


Thanks very much :)


Top
 Profile  
 
 Post subject: Re: Segfault when trying to create a new window GtkWidget
PostPosted: Mon Nov 28, 2011 7:15 pm 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 745
Location: UK
Happy to help. Learning to programme is a case of reading the documentation, looking at other peoples code and seeing if that is easy to read, and asking the right questions in the right way and taking in their advise. So try to explain what the problem is, and if you can put in an example code that can be compiled. Very important do not demand an answer, as far as I know nobody here gets paid to answer questions, we just do it out of the love of programming.

_________________
E.


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 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