Tutorial :Getting a seg fault, having trouble with classes and variables


Ok, so I'm still learning the ropes of C++ here so I apologize if this is a simple mistake.

I have this class:

class RunFrame : public wxFrame {  public:      RunFrame();      void OnKey(wxKeyEvent& keyEvent);    private:      //  Configuration variables.      const wxString *title;      const wxPoint *origin;      const wxSize *size;      const wxColour *background;      const wxColour *foreground;      const wxString *placeholder;        //  Control variables.      wxTextCtrl *command;        //  Event table.      DECLARE_EVENT_TABLE()  };  

...then in the OnKey method I have this code:

void RunFrame::OnKey(wxKeyEvent& keyEvent) {      //  Take the key and process it.      if(WXK_RETURN == keyEvent.GetKeyCode()) {          bool empty = command -> IsEmpty();      }        //  Propogate the event through.      keyEvent.Skip();  }  

...but my program keeps seg faulting when it reaches the line where I attempt to call the IsEmpty method from the command variable. My question is, "Why?" In the constructor of the RunFrame class I can seemingly call methods for the command variable in the same way I'm doing so in the OnKey method...and it compiles correctly, it just seg faults on me when it attempts to execute that line.

Here is the code for the constructor if necessary:

RunFrame::RunFrame() :  wxFrame(NULL, wxID_ANY, wxT("DEFAULT"), wxDefaultPosition, wxDefaultSize, wxBORDER_NONE) {      //  Create the styling constants.      title = new wxString(wxT("RUN"));      origin = new wxPoint(0, 0);      size = new wxSize(250, 25);      background = new wxColour(33, 33, 33);      foreground = new wxColour(255, 255, 255);      placeholder = new wxString(wxT("command"));        //  Set the styling for the frame.      this -> SetTitle(*title);      this -> SetSize(*size);        //  Create the panel and attach the TextControl to it.      wxPanel *panel = new wxPanel(this, wxID_ANY, *origin, *size, wxBORDER_NONE);        //  Create the text control and attach it to the panel.      command = new wxTextCtrl(panel, wxID_ANY, *placeholder, *origin, *size);        //  Set the styling for the text control.      command -> SetBackgroundColour(*background);      command -> SetForegroundColour(*foreground);        //  Connect the key event to the text control.      command -> Connect(wxEVT_CHAR, wxKeyEventHandler(RunFrame::OnKey));        //  Set the focus to the command box.      command -> SetFocus();  }  

Thanks in advance for any help you can give!

Regards, celestialorb


You are catching the event in an object other than the RunFrame object. Probably it's being caught in the base-object of type wxFrame. Use the runtime command wxEvtHandler::Bind<>() to bind the event, rather than an event table, and it should become clear what's happening.

To verify that this is the problem, compare the address of your RunFrame object to the "this" pointer in the OnKey method. Betcha they are different.

UPDATE: Show us your event table definition. That's where the problem is.

UPDATE 2: I've got to leave you with it. Maybe this will be clearer: Your event handler is defined as belonging to RunFrame. The event table is (I think) binding the event to a different object. The effect is that RunFrame::OnKey is being called not with the this-pointer for your RunFrame object, but some other object, probably of a different type. Good luck. Gotta go.


In debugger, check the value of command in OnKey function.

  • Check if command points to an invalid memory ( due to some reason the command object gets deleted)
  • Or if command is pointing to NULL?

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »