msmithy12
Posts: 94
Joined: Fri Aug 10, 2012 8:57 am

GTK segmentation error

Wed May 01, 2013 9:03 am

Ok so i have a large program that works and i then decided to add a few lines of code that would save my variables as a text file in memory.

This routine cause a segmentation error (139) I removed the code to make a the text file and replaced it with printf("save"). Still i had the segmentation error, so i removed all the code except the window the toolbar and the save button and i still get the segmentation error. Im sure i have made a simple error somewhere but i cant see it for looking. I would be grateful to anyone that can scan the code and hopefully spot my error?

Code: Select all

/*
 * segmentation error issue.c
 */

#include <gtk/gtk.h>
#include <stdio.h>
		
void kill_window(GtkWidget *widget, gpointer data) //kill_window event
{
	gtk_main_quit(); //exit program
}
void shutdown(GtkWidget *widget, gpointer data) 
{
	printf("shutdown\n#");
}
void save(GtkWidget *widget, gpointer data) 
{
	printf("save\n");
}
int main(int argc, char *argv[]) 
{


	GtkWidget *window;		//creates a window
	GtkWidget *hbox;		//creates horizontal box
	GtkWidget *hbox1;
	GtkWidget *hbox2;		//creates a second horizontal box
	GtkWidget *vbox;
	GtkWidget *toolbar;
	GtkToolItem *save;
	GtkToolItem	*sep;
	GtkToolItem *exit;
	GtkToolItem *close;

	gtk_init(&argc, &argv);	//argc ARGUMENTcount argv ARGUMENTvalue
//Main Window ###########################################################
	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);	//declares a main window at top level
	gtk_window_set_position(GTK_WINDOW (window), GTK_WIN_POS_CENTER);
	gtk_window_set_title(GTK_WINDOW (window), "Test");	//sets main window title
	gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
	gtk_window_set_keep_above (GTK_WINDOW (window), TRUE);		
	gtk_container_set_border_width	(GTK_CONTAINER (window),2);		
//ToolBar###############################################################
	toolbar = gtk_toolbar_new();
	gtk_toolbar_set_style(GTK_TOOLBAR(toolbar),GTK_TOOLBAR_ICONS);
	gtk_container_set_border_width (GTK_CONTAINER (toolbar),2);	
	save = gtk_tool_button_new_from_stock(GTK_STOCK_SAVE);
	gtk_toolbar_insert(GTK_TOOLBAR(toolbar), save, 0);
	sep = gtk_separator_tool_item_new();
	gtk_toolbar_insert(GTK_TOOLBAR(toolbar), sep, 0);
	close = gtk_tool_button_new_from_stock(GTK_STOCK_CLOSE);
	gtk_toolbar_insert(GTK_TOOLBAR(toolbar), close, 0);
	exit = gtk_tool_button_new_from_stock(GTK_STOCK_QUIT);
	gtk_toolbar_insert(GTK_TOOLBAR(toolbar), exit, 0);
//boxes ##############################################################
	hbox  = gtk_box_new(TRUE, 4);	//hbox is of type gtk_box_new (TRUE=horizontal)
	hbox1 = gtk_box_new(TRUE,4);	//hbox1 is of type gtk_box_new (TRUE=horizontal)
	hbox2 = gtk_box_new(TRUE,4);	//hbox2 is of type gtk_box_new (TRUE=horizontal)
	vbox  = gtk_box_new(FALSE,2);
//connect window signals to routines #################################
	g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(kill_window), NULL);
	g_signal_connect(G_OBJECT(close), "clicked", G_CALLBACK(kill_window), NULL);
	g_signal_connect(G_OBJECT(exit), "clicked", G_CALLBACK(shutdown), NULL);
	g_signal_connect(G_OBJECT(save), "clicked", G_CALLBACK(save), NULL);
//GTK PACKING OPTIONS  ###################################################
	gtk_box_pack_start(GTK_BOX(hbox)	,vbox,		TRUE, TRUE, 2);
	gtk_box_pack_start(GTK_BOX(vbox)	,hbox1,		TRUE, TRUE, 2);
	gtk_box_pack_start(GTK_BOX(vbox)	,hbox2,		TRUE, TRUE, 2);
	gtk_box_pack_start(GTK_BOX(hbox)	,toolbar, 	TRUE, TRUE, 2);		
	gtk_container_add(GTK_CONTAINER(window), hbox);				
	gtk_widget_show_all(window);
	gtk_main();
	return 0;
}
Thanks Msmithy12
I assume I know what I`m talking about... I probably don`t

Home: 256mb Made in UK, Rasbmc, 40" Sony Bravia KDL-40v3000
Work: 2b+, Rasbian, 6" Lilliput touchscreen

raspem
Posts: 26
Joined: Mon Apr 15, 2013 7:14 am

Re: GTK segmentation error

Wed May 01, 2013 10:00 am

Compile it for debugging (-g) and run it in the debugger (gdb). It should point you to exactly where the problem is.
My other computer is a MacBook Pro

msmithy12
Posts: 94
Joined: Fri Aug 10, 2012 8:57 am

Re: GTK segmentation error

Wed May 01, 2013 10:05 am

Code: Select all

program received signal SIGSEGV, segmentation fault.
0x0007e0d0 in ?? ()
i assume that means my error is in 0x0007e0d0

should i understand that?
I assume I know what I`m talking about... I probably don`t

Home: 256mb Made in UK, Rasbmc, 40" Sony Bravia KDL-40v3000
Work: 2b+, Rasbian, 6" Lilliput touchscreen

User avatar
PeterO
Posts: 5958
Joined: Sun Jul 22, 2012 4:14 pm

Re: GTK segmentation error

Wed May 01, 2013 10:58 am

You have two things called "save"...

A global function:
void save(GtkWidget *widget, gpointer data)
{
printf("save\n");
}

A local in the main function:
GtkToolItem *save;

This going to pass the toolitem as the callback address :
g_signal_connect(G_OBJECT(save), "clicked", G_CALLBACK(save), NULL);


Solution is to rename the function to something like "savehandler" and change the g_signal_connect call parameters to

g_signal_connect(G_OBJECT(save), "clicked", G_CALLBACK(savehandler), NULL);


I built the modified code on a Fedora box with
gcc -g -o gtktest gtktest.c $(pkg-config --cflags --libs gtk+-3.0)
and it now works as expected.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

msmithy12
Posts: 94
Joined: Fri Aug 10, 2012 8:57 am

Re: GTK segmentation error

Wed May 01, 2013 12:50 pm

PeterO thank you,

I cant believe i missed that!

msmithy12
I assume I know what I`m talking about... I probably don`t

Home: 256mb Made in UK, Rasbmc, 40" Sony Bravia KDL-40v3000
Work: 2b+, Rasbian, 6" Lilliput touchscreen

Return to “C/C++”