Getting a seg fault, having trouble with classes and variables.

Posted by celestialorb on Stack Overflow See other posts from Stack Overflow or by celestialorb
Published on 2010-04-30T05:03:44Z Indexed on 2010/04/30 5:07 UTC
Read the original article Hit count: 271

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

© Stack Overflow or respective owner

Related posts about c++

Related posts about segmentation-fault