CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2009
    Posts
    56

    Segmentation Fault in FLTK

    Hi, I am getting a segmentation fault in my FLTK code. It only happens on my callback function for a Fl_Choice or Fl_Menu_Button. Here is relevant code -

    in the constructor of my window -
    Code:
    whichSensor = new Fl_Menu_Button(300, 315, 180, 25, "Sensor");
    whichSensor->when(FL_WHEN_RELEASE);
    whichSensor->callback(cb_choice);
    setSensorItems();
    Code:
    void GUIWindow::setSensorItems() {
    
    
        whichSensor->add("Bump", 0, 0, (void*)1);
        whichSensor->add("Wheel_Drop", 0, 0, (void*)1);
        whichSensor->add("Wall", 0, 0, (void*)1);
        whichSensor->add("Cliff_Left", 0, 0, (void*)1);
        whichSensor->add("Cliff_Front_Left", 0, 0, (void*)1);
        whichSensor->add("Cliff_Front_Right", 0, 0, (void*)1);
        whichSensor->add("Cliff_Right", 0, 0, (void*)1);
        whichSensor->add("Virtual_Wall", 0, 0, (void*)1);
        whichSensor->add("Low_Side_Driver", 0, 0, (void*)1);
    }

    Code:
    void GUIWindow::cb_choice(Fl_Widget* o, void* v) {
        GUIWindow* w = (GUIWindow*)v;
        w->cb_choice_i();
    }
    
    void GUIWindow::cb_choice_i() {
        robot->setCurrentSensor(BUMP);
    }
    If I leave the cb_choice_i empty or just have a cout statement in there, everything works fine. But as soon as I try to access a member of the window class, I get a segmentation fault. I have also tried changing v in GUIWindow* w = (GUIWindow*)v; to o, but that also gave me faults. The line in cb_choice_i is just an example; ideally I want to set it to whatever item the user chooses.

    I have done all of my other callback functions this and they all work fine. Here is another for example -
    Code:
    void GUIWindow::cb_toggleSensorStream(Fl_Widget* o, void* v) {
        GUIWindow* w = (GUIWindow*)v;
        w->cb_toggleSensorStream_i();
    }
    
    void GUIWindow::cb_toggleSensorStream_i() {
        robot->toggleSensorStream();
    }
    So I don't understand why I would be getting segmentation faults for the other. My only assumption is that it has something to do with the Fl_Menu_Button, but I don't know what. This problem has been stumping me for some time now so any help at all would be greatly appreciated.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Segmentation Fault in FLTK

    Quote Originally Posted by SterlingM View Post
    If I leave the cb_choice_i empty or just have a cout statement in there, everything works fine. But as soon as I try to access a member of the window class, I get a segmentation fault.
    The pointer is pointing to a bogus object, or the function you're calling through the object is giving you the segmentation fault.

    It's either one or the other. So determine if that pointer is a valid pointer, and if it isn't why it isn't. If it is a valid pointer, then why that function you're calling (setCurrentSensor) crashes.

    Maybe the object it was pointing to no longer exists? Maybe the pointer is not a GUIWindow, but you're assuming it is? You're using a C-style cast, and there is no check to see if that pointer (which is just a void) is actually pointing to a GUIWindow object.

    I don't know what FLTK is, but it still has to follow certain norms, and the norm is that if you get a segmentation fault on a dereferenced pointer, only a few scenarios can cause the issue.

    Edit:
    To add, just because the others work doesn't mean they are correct. If the pointer is indeed invalid, anything can happen, including seemingly "work".

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; June 1st, 2011 at 07:00 PM.

  3. #3
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Segmentation Fault in FLTK

    Quote Originally Posted by Paul McKenzie View Post
    If the pointer is indeed invalid, anything can happen, including seemingly "work".
    To illustrate Paul's point, consider this bit of code.

    Code:
    #include <iostream>
    
    using namespace std;
    
    class C
    {
    public:
        void F1()
        {
            cout << "F1\n";
        }
    
        void F2()
        {
            cout << "F2\n";
            i = 0;
        }
    
    private:
    
        int i;
    };
    
    int main()
    {
        C *p_c = 0;
    
        p_c->F1(); // Works!
        p_c->F2(); // Crash!
    }
    'p_c' does not point to a valid object, but as F1 does not access any member variables, it appears to function correctly.
    F2, on the other hand, tries to access 'i' and fails.

    (Compiled and run under Visual Studio 2008)
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  4. #4
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Segmentation Fault in FLTK

    Quote Originally Posted by JohnW@Wessex View Post
    To illustrate Paul's point, consider this bit of code.

    Code:
    #include <iostream>
    
    using namespace std;
    
    class C
    {
    public:
        void F1()
        {
            cout << "F1\n";
        }
    
        void F2()
        {
            cout << "F2\n";
            i = 0;
        }
    
    private:
    
        int i;
    };
    
    int main()
    {
        C *p_c = 0;
    
        p_c->F1(); // Works!
        p_c->F2(); // Crash!
    }
    'p_c' does not point to a valid object, but as F1 does not access any member variables, it appears to function correctly.
    F2, on the other hand, tries to access 'i' and fails.

    (Compiled and run under Visual Studio 2008)
    Just to add, while "p_c -> F1()" 'works', it is still undefined behavior and may not work in the future.

    Viggy

  5. #5
    Join Date
    Oct 2009
    Posts
    56

    Re: Segmentation Fault in FLTK

    Yes, the void pointer v is null when I enter the function. That is what is causing the segmentation fault (however, I don't know why it would be null). Thank you for the help and replies.

    EDIT: Okay the problem was in

    whichSensor->add("Bump", 0, 0, (void*)1);

    Instead of (void*)1, it should be (void*)this. (void*)1 passes no user_data where (void*)this does.

    Thanks again for the help.
    Last edited by SterlingM; June 2nd, 2011 at 05:53 PM.

  6. #6
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Segmentation Fault in FLTK

    Are you sure that you really need void*? Since I started programming in C++ I've very rarely had need for one. It's often a C idiom used to emulate polymorphism in C++.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  7. #7
    Join Date
    Oct 2009
    Posts
    56

    Re: Segmentation Fault in FLTK

    The example I went by used it and it worked so I just went with it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured