LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-20-2006, 05:33 AM   #1
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Rep: Reputation: 38
segmentation fault?


hi,

I got a segmentation fault when I click the button. I think its has to do with the memory allocation of the struct. Could someone please explain why I get the fault so I wont do the same mistake again.

I fixed the codetag and I saw that I get a null-pointer in data. I wounder why?

Code:
#include <gtk/gtk.h>
typedef enum { APPLE, BANANA } Fruit_Type;

typedef struct Fruit {Fruit_Type ft;} Fruit;


void PealAFruit(Fruit_Type fruit)
{
	g_message ("PealAFruit called");
}

static gboolean button_clicked( GtkWidget      *widget, 
                              GdkEventButton *event,
                              gpointer        data)   /* null here why?*/
{
	Fruit *afruit = (Fruit*)data; 

	PealAFruit(afruit->ft);   /* segmentationfault here */
	return TRUE;
}

int main( int   argc, char *argv[] )
{
    g_message ("start"); 
    GtkWidget *window;
    GtkWidget *button;
    Fruit     *afruit;

    gtk_init (&argc, &argv);

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    button = gtk_button_new_with_label ("Hello World");
 
    if ( (afruit = (Fruit*)g_malloc(sizeof(Fruit))) != NULL) 
	{
		afruit->ft = APPLE;
		g_signal_connect (G_OBJECT (button), "clicked",
		      G_CALLBACK (button_clicked), afruit);
	}

    gtk_container_add (GTK_CONTAINER (window), button);

    gtk_widget_show (button);
    gtk_widget_show (window);
    gtk_main ();
    return 0;
}

Last edited by kalleanka; 11-20-2006 at 11:46 AM.
 
Old 11-20-2006, 10:09 AM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

C++ has this really neat operator called "new". You might want to read about it ;-)
 
Old 11-20-2006, 01:02 PM   #3
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Original Poster
Rep: Reputation: 38
well this is c.
 
Old 11-20-2006, 01:59 PM   #4
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
Never mind Ive removed my post due to its extream retardedness

Last edited by exvor; 11-20-2006 at 02:40 PM.
 
Old 11-20-2006, 02:03 PM   #5
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
I've never used gtk...
Code:
typedef struct Fruit {Fruit_Type ft;} Fruit;
It's a while since I used C, can the tag and the name be the same?
Code:
if ( (afruit = (Fruit*)g_malloc(sizeof(Fruit))) != NULL)
Casting the return of malloc, this is not required and could be at fault.

Why bit machine are you using ? 64?
Code:
g_signal_connect (G_OBJECT (button), "clicked",
		      G_CALLBACK (button_clicked), afruit);
Every example I have seen of gtk casts the data to gpointer.
 
Old 11-20-2006, 02:14 PM   #6
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Sorry - It looked like you were calling GTK+ from C++ and you neglected to initialize the "fruit" variable.

This code works fine for me:
Code:
#include <stdio.h>
#include <string.h>
#include <gtk/gtk.h>

typedef enum
{
  APPLE, BANANA
}
Fruit_Type;

struct Fruit
{
  Fruit_Type ft;
};


void PeelAFruit(Fruit_Type type)
{
  g_message ("PeelAFruit (%d) called", type);
}

static gboolean button_clicked( GtkWidget *widget, gpointer data)
{
    struct Fruit *afruit = (struct Fruit *)data;
    PeelAFruit(afruit->ft);
    return TRUE;
}

int 
main( int argc, char *argv[])
{
    g_message ("start");
    GtkWidget *window;
    GtkWidget *button;
    struct Fruit     *afruit;

    gtk_init (&argc, &argv);

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    button = gtk_button_new_with_label ("Hello World");

    /*
     * NOTE:
     * Per Harmaa Kettu, checking the error status of "g_malloc()" is redundant.
     * Personally, I think if your code might cause a program abort, it would be
     * nice to be explicit about it.  Hence back to standard "malloc()".
     */
    afruit = (struct Fruit *)malloc(sizeof(struct Fruit));
    if (afruit == NULL)
    {
      perror ("Unable to alloc a fruit!");
      abort ();
    }
    afruit->ft = APPLE;
    g_signal_connect (G_OBJECT (button), "clicked",
      G_CALLBACK (button_clicked), afruit);

    gtk_container_add (GTK_CONTAINER (window), button);

    gtk_widget_show (button);
    gtk_widget_show (window);
    gtk_main ();
    return 0;
}
Quote:
gcc -g -I/opt/gnome/include/gtk-2.0 -I/opt/gnome/include/glib-2.0 -I/opt/gnome/lib/gtk-2.0/include -I/opt/gnome/lib/glib-2.0/include -I/opt/gnome/include/pango-1.0 -I/opt/gnome/include/atk-1.0 x.c -o x -L/opt/gnome/lib -L/usr/X11R6/lib -lgtk-x11-2.0 -lgdk-x11-2.0 -lX11 -lXext -lm

** Message: start
** Message: PeelAFruit (1) called

Last edited by paulsm4; 11-20-2006 at 11:17 PM.
 
Old 11-20-2006, 02:39 PM   #7
Harmaa Kettu
Member
 
Registered: Apr 2005
Location: Finland
Posts: 196

Rep: Reputation: 30
From GtkButton documentation:
Code:
"clicked"   void        user_function      (GtkButton *button,
                                            gpointer   user_data)      : Run first / Action
Your callback function has an extra parameter, the GdkButtonEvent shouldn't be there.
Also, you don't need to check the return value of g_malloc(), because g_malloc() aborts the program if there isn't enough free memory. That is the whole point of using it instead of normal malloc().
 
Old 11-20-2006, 03:09 PM   #8
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
Nevermind.......im just going to stop posting on this topic.

Last edited by exvor; 11-20-2006 at 03:13 PM.
 
Old 11-20-2006, 06:30 PM   #9
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Original Poster
Rep: Reputation: 38
thanks a lot for all the tips like:

an extra parameter

and that I do not need to test g_malloc

and the code that works.

Kitos and thanks.

I will work with it tomorrow
 
Old 11-22-2006, 05:42 AM   #10
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
finding seg faults:

one way to do it:

use gdb,
compile your program with -g debugger.
run the program and you dump a 'core' file.

then
gdb program core
and type 'where' it will tell where it was when it failed.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Segmentation fault cmplet-noobie Programming 3 04-03-2006 02:52 AM
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika SUSE / openSUSE 2 12-02-2005 09:34 AM
segmentation fault lmvent Programming 1 10-29-2005 06:34 PM
segmentation fault pippet Programming 4 01-24-2005 01:02 AM
Segmentation fault sin-x Slackware 2 01-12-2005 03:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration