Segmentation fault (core dumped) on ID value Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
alvindera97
Earned a small fee
Earned a small fee
Posts: 20
Joined: Sun Apr 12, 2020 6:11 pm

Segmentation fault (core dumped) on ID value

Post by alvindera97 »

I have been able to write my components properly and draw them to the frame (each component with it's relevant ID at dev time) but when I try to create an event on the event table with the ID of a wxTextCtrl component, I get the error message (after compilation and gtk attemps to run the program):

Code: Select all

Segmentation fault (core dumped) on ID value
I have no idea what the real problem is but I noticed that if I should change the ID bounded to the event handler, OnCourseTextBoxClicked to an ID that doesn't exist. I don't get the error message again, however functionality is not implemented as the given ID is not useful.

Here is my enum:

Code: Select all

enum {
    ID_Hello = 1,
    ID_Course_list = 2,
    ID_New_Calculation = 3,
    ID_Save_Calculation = 4,
    ID_Add_Course_Button = 5,
    ID_Course_Text_Box = 6,
    ID_COURSE_ONE = 11,
    ID_COURSE_ONE_GRADE = 111,
    ID_COURSE_TWO = 12,
    ID_COURSE_TWO_GRADE = 121,
    ID_COURSE_THREE = 13,
    ID_COURSE_THREE_GRADE = 131,
    ID_COURSE_FOUR = 14,
    ID_COURSE_FOUR_GRADE = 141,
    ID_COURSE_FIVE = 15,
    ID_COURSE_FIVE_GRADE = 151,
    ID_COURSE_SIX = 16,
    ID_COURSE_SIX_GRADE = 161,
    ID_COURSE_SEVEN = 17,
    ID_COURSE_SEVEN_GRADE = 171,
    ID_COURSE_EIGHT = 18,
    ID_COURSE_EIGHT_GRADE = 181,
    ID_COURSE_NINE = 19,
    ID_COURSE_NINE_GRADE = 191,
    ID_COURSE_TEN = 110,
    ID_COURSE_TEN_GRADE = 1101,
    ID_COURSE_ELEVEN = 111,
    ID_COURSE_ELEVEN_GRADE = 1111,
    ID_COURSE_TWELVE = 12,
    ID_COURSE_TWELVE_GRADE = 1121,
};
Here is my my frame:

Code: Select all

class MyFrame : public wxFrame {  // defines the options on the top bar of the screen here we have:
    public:
        MyFrame();
    private:
        void OnHello(wxCommandEvent& event);  // hello option
        void OnExit(wxCommandEvent& event);  // exit option
        void OnAbout(wxCommandEvent& event);  // about option
        void OnHelp(wxCommandEvent& event); // event option

        void OnCourseTextBoxClicked(wxCommandEvent &event);

        wxDECLARE_EVENT_TABLE();

    // add course add buttons and list box

    // 100 Level
    wxString level_text = wxT("100 LEVEL");
    wxStaticText *st = new wxStaticText(this, wxID_ANY, level_text, wxPoint(60, 20), wxDefaultSize, wxALIGN_LEFT);
    // wxButton *add_course_button = new wxButton(this, ID_Add_Course_Button, "Add Course", wxPoint(20, 50), wxSize(120, 50));
    // wxTextCtrl *course_text_box = new wxTextCtrl(this, ID_Course_Text_Box, "", wxPoint(5, 105), wxSize(150, 30), wxTE_PROCESS_ENTER);
    // wxListBox *course_list_box = new wxListBox(this, wxID_ANY, wxPoint(5, 140), wxSize(150, 300));

    wxTextCtrl *course_one = new wxTextCtrl(this, ID_COURSE_ONE, "Course 1", wxPoint(5, 44), wxSize(100, 30));
    wxTextCtrl *course_one_grade = new wxTextCtrl(this, ID_COURSE_ONE_GRADE, "Grade", wxPoint(110, 44), wxSize(45, 30));

    wxTextCtrl *course_two = new wxTextCtrl(this, wxID_ANY, "Course 2", wxPoint(5, 76), wxSize(100, 30));
    wxTextCtrl *course_two_grade = new wxTextCtrl(this, wxID_ANY, "Grade", wxPoint(110, 76), wxSize(45, 30));

    wxTextCtrl *course_three = new wxTextCtrl(this, wxID_ANY, "Course 3", wxPoint(5, 108), wxSize(100, 30));
    wxTextCtrl *course_three_grade = new wxTextCtrl(this, wxID_ANY, "Grade", wxPoint(110, 108), wxSize(45, 30));

    wxTextCtrl *course_four = new wxTextCtrl(this, wxID_ANY, "Course 4", wxPoint(5, 140), wxSize(100, 30));
    wxTextCtrl *course_four_grade = new wxTextCtrl(this, wxID_ANY, "Grade", wxPoint(110, 140), wxSize(45, 30));

    wxTextCtrl *course_five = new wxTextCtrl(this, wxID_ANY, "Course 5", wxPoint(5, 172), wxSize(100, 30));
    wxTextCtrl *course_five_grade = new wxTextCtrl(this, wxID_ANY, "Grade", wxPoint(110, 172), wxSize(45, 30));

    wxTextCtrl *course_six = new wxTextCtrl(this, wxID_ANY, "Course 6", wxPoint(5, 204), wxSize(100, 30));
    wxTextCtrl *course_six_grade = new wxTextCtrl(this, wxID_ANY, "Grade", wxPoint(110, 204), wxSize(45, 30));

    wxTextCtrl *course_seven = new wxTextCtrl(this, wxID_ANY, "Course 7", wxPoint(5, 236), wxSize(100, 30));
    wxTextCtrl *course_seven_grade = new wxTextCtrl(this, wxID_ANY, "Grade", wxPoint(110, 236), wxSize(45, 30));

    wxTextCtrl *course_eight = new wxTextCtrl(this, wxID_ANY, "Course 8", wxPoint(5, 268), wxSize(100, 30));
    wxTextCtrl *course_eight_grade = new wxTextCtrl(this, wxID_ANY, "Grade", wxPoint(110, 268), wxSize(45, 30));

    wxTextCtrl *course_nine = new wxTextCtrl(this, wxID_ANY, "Course 9", wxPoint(5, 300), wxSize(100, 30));
    wxTextCtrl *course_nine_grade = new wxTextCtrl(this, wxID_ANY, "Grade", wxPoint(110, 300), wxSize(45, 30));

    wxTextCtrl *course_ten = new wxTextCtrl(this, wxID_ANY, "Course 10", wxPoint(5, 332), wxSize(100, 30));
    wxTextCtrl *course_ten_grade = new wxTextCtrl(this, wxID_ANY, "Grade", wxPoint(110, 332), wxSize(45, 30));

    wxTextCtrl *course_eleven = new wxTextCtrl(this, wxID_ANY, "Course 11", wxPoint(5, 364), wxSize(100, 30));
    wxTextCtrl *course_eleven_grade = new wxTextCtrl(this, wxID_ANY, "Grade", wxPoint(110, 364), wxSize(45, 30));

    wxTextCtrl *course_twelve = new wxTextCtrl(this, wxID_ANY, "Course 12", wxPoint(5, 396), wxSize(100, 30));
    wxTextCtrl *course_twelve_grade = new wxTextCtrl(this, wxID_ANY, "Grade", wxPoint(110, 396), wxSize(45, 30));


    wxButton *calculate_gp = new wxButton(this, wxID_ANY, "Display GP", wxPoint(42.5, 445), wxSize(75, 50));

    // 200 Level
    wxString level_text_2 = wxT("200 LEVEL");
    wxStaticText *st_2 = new wxStaticText(this, wxID_ANY, level_text_2, wxPoint(210, 20), wxDefaultSize, wxALIGN_LEFT);
    wxButton *add_course_button_2 = new wxButton(this, ID_Add_Course_Button, "Add Course", wxPoint(185, 50), wxSize(120, 50));
    wxTextCtrl *course_text_box_2 = new wxTextCtrl(this, wxID_ANY, "", wxPoint(170, 105), wxSize(150, 30), wxTE_PROCESS_ENTER);
    wxListBox *course_list_box_2 = new wxListBox(this, wxID_ANY, wxPoint(170, 140), wxSize(150, 300));
    wxButton *calculate_gp_2 = new wxButton(this, wxID_ANY, "Display GP", wxPoint(207.5, 445), wxSize(75, 50));

    // 300 Level
    wxString level_text_3 = wxT("300 LEVEL");
    wxStaticText *st_3 = new wxStaticText(this, wxID_ANY, level_text_3, wxPoint(390, 20), wxDefaultSize, wxALIGN_LEFT);
    wxButton *add_course_button_3 = new wxButton(this, ID_Add_Course_Button, "Add Course", wxPoint(350, 50), wxSize(120, 50));
    wxTextCtrl *course_text_box_3 = new wxTextCtrl(this, wxID_ANY, "", wxPoint(335, 105), wxSize(150, 30), wxTE_PROCESS_ENTER);
    wxListBox *course_list_box_3 = new wxListBox(this, wxID_ANY, wxPoint(335, 140), wxSize(150, 300));
    wxButton *calculate_gp_3 = new wxButton(this, wxID_ANY, "Display GP", wxPoint(372.5, 445), wxSize(75, 50));

    // 400 Level
    wxString level_text_4 = wxT("400 LEVEL");
    wxStaticText *st_4 = new wxStaticText(this, wxID_ANY, level_text_4, wxPoint(555, 20), wxDefaultSize, wxALIGN_LEFT);
    wxButton *add_course_button_4 = new wxButton(this, ID_Add_Course_Button, "Add Course", wxPoint(515, 50), wxSize(120, 50));
    wxTextCtrl *course_text_box_4 = new wxTextCtrl(this, wxID_ANY, "", wxPoint(500, 105), wxSize(150, 30), wxTE_PROCESS_ENTER);
    wxListBox *course_list_box_4 = new wxListBox(this, wxID_ANY, wxPoint(500, 140), wxSize(150, 300));
    wxButton *calculate_gp_4 = new wxButton(this, wxID_ANY, "Display GP", wxPoint(537.5, 445), wxSize(75, 50));

    // 500 Level
    wxString level_text_5 = wxT("500 LEVEL");
    wxStaticText *st_5 = new wxStaticText(this, wxID_ANY, level_text_5, wxPoint(720, 20), wxDefaultSize, wxALIGN_LEFT);
    wxButton *add_course_button_5 = new wxButton(this, ID_Add_Course_Button, "Add Course", wxPoint(680, 50), wxSize(120, 50));
    wxTextCtrl *course_text_box_5 = new wxTextCtrl(this, wxID_ANY, "", wxPoint(665, 105), wxSize(150, 30), wxTE_PROCESS_ENTER);
    wxListBox *course_list_box_5 = new wxListBox(this, wxID_ANY, wxPoint(665, 140), wxSize(150, 300));
    wxButton *calculate_gp_5 = new wxButton(this, wxID_ANY, "Display GP", wxPoint(702.5, 445), wxSize(75, 50));
};
Here is my evt table:

Code: Select all

wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
    EVT_TEXT(ID_COURSE_ONE, MyFrame::OnCourseTextBoxClicked)
wxEND_EVENT_TABLE()
Here is my event handler:

Code: Select all

void MyFrame::OnCourseTextBoxClicked(wxCommandEvent& event)
{
    course_one->SetMaxLength(6);
    course_one_grade->SetMaxLength(1);
    event.Skip();
};
All together, my entire codebase goes thus:

Code: Select all

// wxWidgets "Hello World" Program
// For compilers that support precompilation, includes "wx/wx.h".
#include <wx-3.1/wx/wx.h>
#include <wx-3.1/wx/textctrl.h>
#include <wx-3.1/wx/wxprec.h>
#include <wx-3.1/wx/button.h>
#include <wx-3.1/wx/stattext.h>

#ifndef WX_PRECOMP
    #include <wx-3.1/wx/wx.h>
#endif

#include <iostream>
#include <string>

//g++ hello_world.cpp `wx-config --cxxflags --libs` -o gpCalculator --verbose
//g++ hello_world.cpp `wx-config --cxxflags --libs` -o gpCalculator --verbose && ./gpCalculator

enum {
    ID_Hello = 1,
    ID_Course_list = 2,
    ID_New_Calculation = 3,
    ID_Save_Calculation = 4,
    ID_Add_Course_Button = 5,
    ID_Course_Text_Box = 6,
    ID_COURSE_ONE = 11,
    ID_COURSE_ONE_GRADE = 111,
    ID_COURSE_TWO = 12,
    ID_COURSE_TWO_GRADE = 121,
    ID_COURSE_THREE = 13,
    ID_COURSE_THREE_GRADE = 131,
    ID_COURSE_FOUR = 14,
    ID_COURSE_FOUR_GRADE = 141,
    ID_COURSE_FIVE = 15,
    ID_COURSE_FIVE_GRADE = 151,
    ID_COURSE_SIX = 16,
    ID_COURSE_SIX_GRADE = 161,
    ID_COURSE_SEVEN = 17,
    ID_COURSE_SEVEN_GRADE = 171,
    ID_COURSE_EIGHT = 18,
    ID_COURSE_EIGHT_GRADE = 181,
    ID_COURSE_NINE = 19,
    ID_COURSE_NINE_GRADE = 191,
    ID_COURSE_TEN = 110,
    ID_COURSE_TEN_GRADE = 1101,
    ID_COURSE_ELEVEN = 111,
    ID_COURSE_ELEVEN_GRADE = 1111,
    ID_COURSE_TWELVE = 12,
    ID_COURSE_TWELVE_GRADE = 1121,
};

class GpCalculator : public wxApp {
    public:
        virtual bool OnInit();

};


class MyFrame : public wxFrame {  // defines the options on the top bar of the screen here we have:
    public:
        MyFrame();
    private:
        void OnHello(wxCommandEvent& event);  // hello option
        void OnExit(wxCommandEvent& event);  // exit option
        void OnAbout(wxCommandEvent& event);  // about option
        void OnHelp(wxCommandEvent& event); // event option

        void OnCourseTextBoxClicked(wxCommandEvent &event);

        wxDECLARE_EVENT_TABLE();

    // add course add buttons and list box

    // 100 Level
    wxString level_text = wxT("100 LEVEL");
    wxStaticText *st = new wxStaticText(this, wxID_ANY, level_text, wxPoint(60, 20), wxDefaultSize, wxALIGN_LEFT);
    // wxButton *add_course_button = new wxButton(this, ID_Add_Course_Button, "Add Course", wxPoint(20, 50), wxSize(120, 50));
    // wxTextCtrl *course_text_box = new wxTextCtrl(this, ID_Course_Text_Box, "", wxPoint(5, 105), wxSize(150, 30), wxTE_PROCESS_ENTER);
    // wxListBox *course_list_box = new wxListBox(this, wxID_ANY, wxPoint(5, 140), wxSize(150, 300));

    wxTextCtrl *course_one = new wxTextCtrl(this, ID_COURSE_ONE, "Course 1", wxPoint(5, 44), wxSize(100, 30));
    wxTextCtrl *course_one_grade = new wxTextCtrl(this, ID_COURSE_ONE_GRADE, "Grade", wxPoint(110, 44), wxSize(45, 30));

    wxTextCtrl *course_two = new wxTextCtrl(this, wxID_ANY, "Course 2", wxPoint(5, 76), wxSize(100, 30));
    wxTextCtrl *course_two_grade = new wxTextCtrl(this, wxID_ANY, "Grade", wxPoint(110, 76), wxSize(45, 30));

    wxTextCtrl *course_three = new wxTextCtrl(this, wxID_ANY, "Course 3", wxPoint(5, 108), wxSize(100, 30));
    wxTextCtrl *course_three_grade = new wxTextCtrl(this, wxID_ANY, "Grade", wxPoint(110, 108), wxSize(45, 30));

    wxTextCtrl *course_four = new wxTextCtrl(this, wxID_ANY, "Course 4", wxPoint(5, 140), wxSize(100, 30));
    wxTextCtrl *course_four_grade = new wxTextCtrl(this, wxID_ANY, "Grade", wxPoint(110, 140), wxSize(45, 30));

    wxTextCtrl *course_five = new wxTextCtrl(this, wxID_ANY, "Course 5", wxPoint(5, 172), wxSize(100, 30));
    wxTextCtrl *course_five_grade = new wxTextCtrl(this, wxID_ANY, "Grade", wxPoint(110, 172), wxSize(45, 30));

    wxTextCtrl *course_six = new wxTextCtrl(this, wxID_ANY, "Course 6", wxPoint(5, 204), wxSize(100, 30));
    wxTextCtrl *course_six_grade = new wxTextCtrl(this, wxID_ANY, "Grade", wxPoint(110, 204), wxSize(45, 30));

    wxTextCtrl *course_seven = new wxTextCtrl(this, wxID_ANY, "Course 7", wxPoint(5, 236), wxSize(100, 30));
    wxTextCtrl *course_seven_grade = new wxTextCtrl(this, wxID_ANY, "Grade", wxPoint(110, 236), wxSize(45, 30));

    wxTextCtrl *course_eight = new wxTextCtrl(this, wxID_ANY, "Course 8", wxPoint(5, 268), wxSize(100, 30));
    wxTextCtrl *course_eight_grade = new wxTextCtrl(this, wxID_ANY, "Grade", wxPoint(110, 268), wxSize(45, 30));

    wxTextCtrl *course_nine = new wxTextCtrl(this, wxID_ANY, "Course 9", wxPoint(5, 300), wxSize(100, 30));
    wxTextCtrl *course_nine_grade = new wxTextCtrl(this, wxID_ANY, "Grade", wxPoint(110, 300), wxSize(45, 30));

    wxTextCtrl *course_ten = new wxTextCtrl(this, wxID_ANY, "Course 10", wxPoint(5, 332), wxSize(100, 30));
    wxTextCtrl *course_ten_grade = new wxTextCtrl(this, wxID_ANY, "Grade", wxPoint(110, 332), wxSize(45, 30));

    wxTextCtrl *course_eleven = new wxTextCtrl(this, wxID_ANY, "Course 11", wxPoint(5, 364), wxSize(100, 30));
    wxTextCtrl *course_eleven_grade = new wxTextCtrl(this, wxID_ANY, "Grade", wxPoint(110, 364), wxSize(45, 30));

    wxTextCtrl *course_twelve = new wxTextCtrl(this, wxID_ANY, "Course 12", wxPoint(5, 396), wxSize(100, 30));
    wxTextCtrl *course_twelve_grade = new wxTextCtrl(this, wxID_ANY, "Grade", wxPoint(110, 396), wxSize(45, 30));


    wxButton *calculate_gp = new wxButton(this, wxID_ANY, "Display GP", wxPoint(42.5, 445), wxSize(75, 50));

    // 200 Level
    wxString level_text_2 = wxT("200 LEVEL");
    wxStaticText *st_2 = new wxStaticText(this, wxID_ANY, level_text_2, wxPoint(210, 20), wxDefaultSize, wxALIGN_LEFT);
    wxButton *add_course_button_2 = new wxButton(this, ID_Add_Course_Button, "Add Course", wxPoint(185, 50), wxSize(120, 50));
    wxTextCtrl *course_text_box_2 = new wxTextCtrl(this, wxID_ANY, "", wxPoint(170, 105), wxSize(150, 30), wxTE_PROCESS_ENTER);
    wxListBox *course_list_box_2 = new wxListBox(this, wxID_ANY, wxPoint(170, 140), wxSize(150, 300));
    wxButton *calculate_gp_2 = new wxButton(this, wxID_ANY, "Display GP", wxPoint(207.5, 445), wxSize(75, 50));

    // 300 Level
    wxString level_text_3 = wxT("300 LEVEL");
    wxStaticText *st_3 = new wxStaticText(this, wxID_ANY, level_text_3, wxPoint(390, 20), wxDefaultSize, wxALIGN_LEFT);
    wxButton *add_course_button_3 = new wxButton(this, ID_Add_Course_Button, "Add Course", wxPoint(350, 50), wxSize(120, 50));
    wxTextCtrl *course_text_box_3 = new wxTextCtrl(this, wxID_ANY, "", wxPoint(335, 105), wxSize(150, 30), wxTE_PROCESS_ENTER);
    wxListBox *course_list_box_3 = new wxListBox(this, wxID_ANY, wxPoint(335, 140), wxSize(150, 300));
    wxButton *calculate_gp_3 = new wxButton(this, wxID_ANY, "Display GP", wxPoint(372.5, 445), wxSize(75, 50));

    // 400 Level
    wxString level_text_4 = wxT("400 LEVEL");
    wxStaticText *st_4 = new wxStaticText(this, wxID_ANY, level_text_4, wxPoint(555, 20), wxDefaultSize, wxALIGN_LEFT);
    wxButton *add_course_button_4 = new wxButton(this, ID_Add_Course_Button, "Add Course", wxPoint(515, 50), wxSize(120, 50));
    wxTextCtrl *course_text_box_4 = new wxTextCtrl(this, wxID_ANY, "", wxPoint(500, 105), wxSize(150, 30), wxTE_PROCESS_ENTER);
    wxListBox *course_list_box_4 = new wxListBox(this, wxID_ANY, wxPoint(500, 140), wxSize(150, 300));
    wxButton *calculate_gp_4 = new wxButton(this, wxID_ANY, "Display GP", wxPoint(537.5, 445), wxSize(75, 50));

    // 500 Level
    wxString level_text_5 = wxT("500 LEVEL");
    wxStaticText *st_5 = new wxStaticText(this, wxID_ANY, level_text_5, wxPoint(720, 20), wxDefaultSize, wxALIGN_LEFT);
    wxButton *add_course_button_5 = new wxButton(this, ID_Add_Course_Button, "Add Course", wxPoint(680, 50), wxSize(120, 50));
    wxTextCtrl *course_text_box_5 = new wxTextCtrl(this, wxID_ANY, "", wxPoint(665, 105), wxSize(150, 30), wxTE_PROCESS_ENTER);
    wxListBox *course_list_box_5 = new wxListBox(this, wxID_ANY, wxPoint(665, 140), wxSize(150, 300));
    wxButton *calculate_gp_5 = new wxButton(this, wxID_ANY, "Display GP", wxPoint(702.5, 445), wxSize(75, 50));
};

wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
    EVT_TEXT(ID_COURSE_ONE, MyFrame::OnCourseTextBoxClicked)
wxEND_EVENT_TABLE()


wxIMPLEMENT_APP(GpCalculator);   // initiate main()

bool GpCalculator::OnInit() {  // append the OnInit() function
    MyFrame *frame = new MyFrame();
    frame->Show(true);
    return true;  // this is just to show successfull initialisation
}


MyFrame::MyFrame() : wxFrame(nullptr, wxID_ANY, "Engineering Grade Pending Calculator", wxPoint(100, 100), wxSize(820, 600)) {  // Frame of the program
    wxMenu *menuFile = new wxMenu;
    menuFile->AppendSeparator();
    menuFile->Append(wxID_NEW);
    menuFile->AppendSeparator();
    menuFile->Append(wxID_SAVE);
    menuFile->AppendSeparator();
    menuFile->Append(wxID_SAVEAS);
    menuFile->AppendSeparator();
    menuFile->Append(wxID_UNDO);
    menuFile->AppendSeparator();
    menuFile->Append(wxID_REDO);
    menuFile->AppendSeparator();

    menuFile->Append(wxID_EXIT);

    wxMenu *menuCourses = new wxMenu;
    menuCourses->Append(ID_Course_list, "&All Courses");
    menuCourses->AppendSeparator();
    menuCourses->Append(wxID_ANY, "&New Course");

    wxMenu *menuHelp = new wxMenu;
    menuHelp->Append(wxID_ABOUT);
    menuHelp->AppendSeparator();
    menuHelp->Append(wxID_HELP);

    wxMenuBar *menuBar = new wxMenuBar;  // create the menubar where menus would live

    // append menus to the menubar
    menuBar->Append(menuFile, "&File");  
    menuBar->Append(menuCourses, "&Course");
    menuBar->Append(menuHelp, "&Help");

    SetMenuBar( menuBar );
    CreateStatusBar();
    SetStatusText("Welcome to wxWidgets!");
    Bind(wxEVT_MENU, &MyFrame::OnHello, this, ID_Hello);
    Bind(wxEVT_MENU, &MyFrame::OnAbout, this, wxID_ABOUT);
    Bind(wxEVT_MENU, &MyFrame::OnExit, this, wxID_EXIT);
    Bind(wxEVT_MENU, &MyFrame::OnHelp, this, wxID_HELP);

}

void MyFrame::OnExit(wxCommandEvent& event) {
    Close(true);
}

void MyFrame::OnAbout(wxCommandEvent& event) {
    wxMessageBox("This is a programme for calculating the GP of an Engineering student at the Univesity of Benin.",
                 "GP Calculator V0.0.1", wxOK | wxICON_INFORMATION);
}

void MyFrame::OnHello(wxCommandEvent& event) {
    wxLogMessage("Hello world from wxWidgets!");
}

void MyFrame::OnHelp(wxCommandEvent& event) {
    wxMessageBox("For help on how to use this programme, please use the manual");
}

void MyFrame::OnCourseTextBoxClicked(wxCommandEvent& event)
{
    course_one->SetMaxLength(6);
    course_one_grade->SetMaxLength(1);
    event.Skip();
};
I am using linux and I installed wxwidgets from source as described in the installation page. All components work properly.

This is the command I use to compile and run the programme:

Code: Select all

 g++ hello_world.cpp `wx-config --cxxflags --libs` -o gpCalculator --verbose && ./gpCalculator
I'm relatively new to C++ and I am basically relying on my years of experience with Python programming and Object Oriented Python Thank you!
catalin
Moderator
Moderator
Posts: 1618
Joined: Wed Nov 12, 2008 7:23 am
Location: Romania

Re: Segmentation fault (core dumped) on ID value

Post by catalin »

Code: Select all

MyFrame *frame = new MyFrame();
There's your problem right there. Let's see if you understand it and then solve it.

You really, really need to learn about c++ and wxWidgets first. Needless to say there is a lot of documentation online, and the samples that come with wxWidgets should be of great help.
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Segmentation fault (core dumped) on ID value

Post by doublemax »

catalin wrote: Tue Apr 14, 2020 6:27 am

Code: Select all

MyFrame *frame = new MyFrame();
There's your problem right there. Let's see if you understand it and then solve it.
@catalin: That's not really helpful. Even i didn't understand what you mean by that.

There are several issues with your code.

a) Change your include paths, so that you don't have to include the 'wx-3.1' part in it. The #includes need to look like this:

Code: Select all

#include <wx/wx.h>
#include <wx/textctrl.h>
#include <wx/wxprec.h>
#include <wx/button.h>
#include <wx/stattext.h>
b) The generation of the controls should happen in the MyFrame constructor. The constructor does what __init__ does in Python.

c) Under Linux even constructing a wxTextCtrl will cause an wxEVT_TEXT event. That means that the event handler "OnCourseTextBoxClicked" will be called before the member variable "course_one" is initialized. (This is the actual cause for the crash). Therefore, don't use a static event table for that event, use Bind() like for the others.

d) additionally, the variables "course_one" and "course_one_grade" have to be member variables, so that you can access them from inside the event handler

Here's the corrected code, please study the changes i made.

Code: Select all

// wxWidgets "Hello World" Program
// For compilers that support precompilation, includes "wx/wx.h".
#include <wx/wx.h>
#include <wx/textctrl.h>
#include <wx/wxprec.h>
#include <wx/button.h>
#include <wx/stattext.h>

#ifndef WX_PRECOMP
    #include <wx/wx.h>
#endif

#include <iostream>
#include <string>

//g++ hello_world.cpp `wx-config --cxxflags --libs` -o gpCalculator --verbose
//g++ hello_world.cpp `wx-config --cxxflags --libs` -o gpCalculator --verbose && ./gpCalculator

enum {
    ID_Hello = 1,
    ID_Course_list = 2,
    ID_New_Calculation = 3,
    ID_Save_Calculation = 4,
    ID_Add_Course_Button = 5,
    ID_Course_Text_Box = 6,
    ID_COURSE_ONE = 11,
    ID_COURSE_ONE_GRADE = 111,
    ID_COURSE_TWO = 12,
    ID_COURSE_TWO_GRADE = 121,
    ID_COURSE_THREE = 13,
    ID_COURSE_THREE_GRADE = 131,
    ID_COURSE_FOUR = 14,
    ID_COURSE_FOUR_GRADE = 141,
    ID_COURSE_FIVE = 15,
    ID_COURSE_FIVE_GRADE = 151,
    ID_COURSE_SIX = 16,
    ID_COURSE_SIX_GRADE = 161,
    ID_COURSE_SEVEN = 17,
    ID_COURSE_SEVEN_GRADE = 171,
    ID_COURSE_EIGHT = 18,
    ID_COURSE_EIGHT_GRADE = 181,
    ID_COURSE_NINE = 19,
    ID_COURSE_NINE_GRADE = 191,
    ID_COURSE_TEN = 110,
    ID_COURSE_TEN_GRADE = 1101,
    ID_COURSE_ELEVEN = 111,
    ID_COURSE_ELEVEN_GRADE = 1111,
    ID_COURSE_TWELVE = 12,
    ID_COURSE_TWELVE_GRADE = 1121,
};

class GpCalculator : public wxApp {
    public:
        virtual bool OnInit();

};


class MyFrame : public wxFrame {  // defines the options on the top bar of the screen here we have:
    public:
        MyFrame();
    private:
        void OnHello(wxCommandEvent& event);  // hello option
        void OnExit(wxCommandEvent& event);  // exit option
        void OnAbout(wxCommandEvent& event);  // about option
        void OnHelp(wxCommandEvent& event); // event option

        void OnCourseTextBoxClicked(wxCommandEvent &event);

        wxTextCtrl *course_one;
        wxTextCtrl *course_one_grade;

        wxDECLARE_EVENT_TABLE();
};

wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
    // EVT_TEXT(ID_COURSE_ONE, MyFrame::OnCourseTextBoxClicked)
wxEND_EVENT_TABLE()


wxIMPLEMENT_APP(GpCalculator);   // initiate main()

bool GpCalculator::OnInit() {  // append the OnInit() function
    MyFrame *frame = new MyFrame();
    frame->Show(true);
    return true;  // this is just to show successfull initialisation
}


MyFrame::MyFrame() : wxFrame(nullptr, wxID_ANY, "Engineering Grade Pending Calculator", wxPoint(100, 100), wxSize(820, 600)) {  // Frame of the program
    wxMenu *menuFile = new wxMenu;
    menuFile->AppendSeparator();
    menuFile->Append(wxID_NEW);
    menuFile->AppendSeparator();
    menuFile->Append(wxID_SAVE);
    menuFile->AppendSeparator();
    menuFile->Append(wxID_SAVEAS);
    menuFile->AppendSeparator();
    menuFile->Append(wxID_UNDO);
    menuFile->AppendSeparator();
    menuFile->Append(wxID_REDO);
    menuFile->AppendSeparator();

    menuFile->Append(wxID_EXIT);

    wxMenu *menuCourses = new wxMenu;
    menuCourses->Append(ID_Course_list, "&All Courses");
    menuCourses->AppendSeparator();
    menuCourses->Append(wxID_ANY, "&New Course");

    wxMenu *menuHelp = new wxMenu;
    menuHelp->Append(wxID_ABOUT);
    menuHelp->AppendSeparator();
    menuHelp->Append(wxID_HELP);

    wxMenuBar *menuBar = new wxMenuBar;  // create the menubar where menus would live

    // append menus to the menubar
    menuBar->Append(menuFile, "&File");  
    menuBar->Append(menuCourses, "&Course");
    menuBar->Append(menuHelp, "&Help");

    SetMenuBar( menuBar );
    CreateStatusBar();
    SetStatusText("Welcome to wxWidgets!");

// add course add buttons and list box

    // 100 Level
    wxString level_text = wxT("100 LEVEL");
    wxStaticText *st = new wxStaticText(this, wxID_ANY, level_text, wxPoint(60, 20), wxDefaultSize, wxALIGN_LEFT);
    // wxButton *add_course_button = new wxButton(this, ID_Add_Course_Button, "Add Course", wxPoint(20, 50), wxSize(120, 50));
    // wxTextCtrl *course_text_box = new wxTextCtrl(this, ID_Course_Text_Box, "", wxPoint(5, 105), wxSize(150, 30), wxTE_PROCESS_ENTER);
    // wxListBox *course_list_box = new wxListBox(this, wxID_ANY, wxPoint(5, 140), wxSize(150, 300));

    course_one = new wxTextCtrl(this, ID_COURSE_ONE, "Course 1", wxPoint(5, 44), wxSize(100, 30));
    course_one_grade = new wxTextCtrl(this, ID_COURSE_ONE_GRADE, "Grade", wxPoint(110, 44), wxSize(45, 30));

    wxTextCtrl *course_two = new wxTextCtrl(this, wxID_ANY, "Course 2", wxPoint(5, 76), wxSize(100, 30));
    wxTextCtrl *course_two_grade = new wxTextCtrl(this, wxID_ANY, "Grade", wxPoint(110, 76), wxSize(45, 30));

    wxTextCtrl *course_three = new wxTextCtrl(this, wxID_ANY, "Course 3", wxPoint(5, 108), wxSize(100, 30));
    wxTextCtrl *course_three_grade = new wxTextCtrl(this, wxID_ANY, "Grade", wxPoint(110, 108), wxSize(45, 30));

    wxTextCtrl *course_four = new wxTextCtrl(this, wxID_ANY, "Course 4", wxPoint(5, 140), wxSize(100, 30));
    wxTextCtrl *course_four_grade = new wxTextCtrl(this, wxID_ANY, "Grade", wxPoint(110, 140), wxSize(45, 30));

    wxTextCtrl *course_five = new wxTextCtrl(this, wxID_ANY, "Course 5", wxPoint(5, 172), wxSize(100, 30));
    wxTextCtrl *course_five_grade = new wxTextCtrl(this, wxID_ANY, "Grade", wxPoint(110, 172), wxSize(45, 30));

    wxTextCtrl *course_six = new wxTextCtrl(this, wxID_ANY, "Course 6", wxPoint(5, 204), wxSize(100, 30));
    wxTextCtrl *course_six_grade = new wxTextCtrl(this, wxID_ANY, "Grade", wxPoint(110, 204), wxSize(45, 30));

    wxTextCtrl *course_seven = new wxTextCtrl(this, wxID_ANY, "Course 7", wxPoint(5, 236), wxSize(100, 30));
    wxTextCtrl *course_seven_grade = new wxTextCtrl(this, wxID_ANY, "Grade", wxPoint(110, 236), wxSize(45, 30));

    wxTextCtrl *course_eight = new wxTextCtrl(this, wxID_ANY, "Course 8", wxPoint(5, 268), wxSize(100, 30));
    wxTextCtrl *course_eight_grade = new wxTextCtrl(this, wxID_ANY, "Grade", wxPoint(110, 268), wxSize(45, 30));

    wxTextCtrl *course_nine = new wxTextCtrl(this, wxID_ANY, "Course 9", wxPoint(5, 300), wxSize(100, 30));
    wxTextCtrl *course_nine_grade = new wxTextCtrl(this, wxID_ANY, "Grade", wxPoint(110, 300), wxSize(45, 30));

    wxTextCtrl *course_ten = new wxTextCtrl(this, wxID_ANY, "Course 10", wxPoint(5, 332), wxSize(100, 30));
    wxTextCtrl *course_ten_grade = new wxTextCtrl(this, wxID_ANY, "Grade", wxPoint(110, 332), wxSize(45, 30));

    wxTextCtrl *course_eleven = new wxTextCtrl(this, wxID_ANY, "Course 11", wxPoint(5, 364), wxSize(100, 30));
    wxTextCtrl *course_eleven_grade = new wxTextCtrl(this, wxID_ANY, "Grade", wxPoint(110, 364), wxSize(45, 30));

    wxTextCtrl *course_twelve = new wxTextCtrl(this, wxID_ANY, "Course 12", wxPoint(5, 396), wxSize(100, 30));
    wxTextCtrl *course_twelve_grade = new wxTextCtrl(this, wxID_ANY, "Grade", wxPoint(110, 396), wxSize(45, 30));


    wxButton *calculate_gp = new wxButton(this, wxID_ANY, "Display GP", wxPoint(42.5, 445), wxSize(75, 50));

    // 200 Level
    wxString level_text_2 = wxT("200 LEVEL");
    wxStaticText *st_2 = new wxStaticText(this, wxID_ANY, level_text_2, wxPoint(210, 20), wxDefaultSize, wxALIGN_LEFT);
    wxButton *add_course_button_2 = new wxButton(this, ID_Add_Course_Button, "Add Course", wxPoint(185, 50), wxSize(120, 50));
    wxTextCtrl *course_text_box_2 = new wxTextCtrl(this, wxID_ANY, "", wxPoint(170, 105), wxSize(150, 30), wxTE_PROCESS_ENTER);
    wxListBox *course_list_box_2 = new wxListBox(this, wxID_ANY, wxPoint(170, 140), wxSize(150, 300));
    wxButton *calculate_gp_2 = new wxButton(this, wxID_ANY, "Display GP", wxPoint(207.5, 445), wxSize(75, 50));

    // 300 Level
    wxString level_text_3 = wxT("300 LEVEL");
    wxStaticText *st_3 = new wxStaticText(this, wxID_ANY, level_text_3, wxPoint(390, 20), wxDefaultSize, wxALIGN_LEFT);
    wxButton *add_course_button_3 = new wxButton(this, ID_Add_Course_Button, "Add Course", wxPoint(350, 50), wxSize(120, 50));
    wxTextCtrl *course_text_box_3 = new wxTextCtrl(this, wxID_ANY, "", wxPoint(335, 105), wxSize(150, 30), wxTE_PROCESS_ENTER);
    wxListBox *course_list_box_3 = new wxListBox(this, wxID_ANY, wxPoint(335, 140), wxSize(150, 300));
    wxButton *calculate_gp_3 = new wxButton(this, wxID_ANY, "Display GP", wxPoint(372.5, 445), wxSize(75, 50));

    // 400 Level
    wxString level_text_4 = wxT("400 LEVEL");
    wxStaticText *st_4 = new wxStaticText(this, wxID_ANY, level_text_4, wxPoint(555, 20), wxDefaultSize, wxALIGN_LEFT);
    wxButton *add_course_button_4 = new wxButton(this, ID_Add_Course_Button, "Add Course", wxPoint(515, 50), wxSize(120, 50));
    wxTextCtrl *course_text_box_4 = new wxTextCtrl(this, wxID_ANY, "", wxPoint(500, 105), wxSize(150, 30), wxTE_PROCESS_ENTER);
    wxListBox *course_list_box_4 = new wxListBox(this, wxID_ANY, wxPoint(500, 140), wxSize(150, 300));
    wxButton *calculate_gp_4 = new wxButton(this, wxID_ANY, "Display GP", wxPoint(537.5, 445), wxSize(75, 50));

    // 500 Level
    wxString level_text_5 = wxT("500 LEVEL");
    wxStaticText *st_5 = new wxStaticText(this, wxID_ANY, level_text_5, wxPoint(720, 20), wxDefaultSize, wxALIGN_LEFT);
    wxButton *add_course_button_5 = new wxButton(this, ID_Add_Course_Button, "Add Course", wxPoint(680, 50), wxSize(120, 50));
    wxTextCtrl *course_text_box_5 = new wxTextCtrl(this, wxID_ANY, "", wxPoint(665, 105), wxSize(150, 30), wxTE_PROCESS_ENTER);
    wxListBox *course_list_box_5 = new wxListBox(this, wxID_ANY, wxPoint(665, 140), wxSize(150, 300));
    wxButton *calculate_gp_5 = new wxButton(this, wxID_ANY, "Display GP", wxPoint(702.5, 445), wxSize(75, 50));

    Bind(wxEVT_MENU, &MyFrame::OnHello, this, ID_Hello);
    Bind(wxEVT_MENU, &MyFrame::OnAbout, this, wxID_ABOUT);
    Bind(wxEVT_MENU, &MyFrame::OnExit, this, wxID_EXIT);
    Bind(wxEVT_MENU, &MyFrame::OnHelp, this, wxID_HELP);

    Bind(wxEVT_TEXT, &MyFrame::OnCourseTextBoxClicked, this, ID_COURSE_ONE);
}

void MyFrame::OnExit(wxCommandEvent& event) {
    Close(true);
}

void MyFrame::OnAbout(wxCommandEvent& event) {
    wxMessageBox("This is a programme for calculating the GP of an Engineering student at the Univesity of Benin.",
                 "GP Calculator V0.0.1", wxOK | wxICON_INFORMATION);
}

void MyFrame::OnHello(wxCommandEvent& event) {
    wxLogMessage("Hello world from wxWidgets!");
}

void MyFrame::OnHelp(wxCommandEvent& event) {
    wxMessageBox("For help on how to use this programme, please use the manual");
}

void MyFrame::OnCourseTextBoxClicked(wxCommandEvent& event)
{
    course_one->SetMaxLength(6);
    course_one_grade->SetMaxLength(1);
};
Use the source, Luke!
catalin
Moderator
Moderator
Posts: 1618
Joined: Wed Nov 12, 2008 7:23 am
Location: Romania

Re: Segmentation fault (core dumped) on ID value

Post by catalin »

doublemax wrote: Tue Apr 14, 2020 8:03 am
catalin wrote: Tue Apr 14, 2020 6:27 am

Code: Select all

MyFrame *frame = new MyFrame();
There's your problem right there. Let's see if you understand it and then solve it.
@catalin: That's not really helpful. Even i didn't understand what you mean by that.
Actually I was wrong there.
I meant that a frame that was not yet created was being used, but failed to realize that MyFrame() was actually calling the correct constructor (I was sure I scrolled all the way down but apparently not). Sorry for being hasty.

And...
e) User ids should be above wxID_HIGHEST.


p.s. I still think though that understanding basic feature of a language (and probably the framework too) is a decent "must" before asking people to correct 230+ lines of code. Writing the code for them, on the other hand, is most likely not the best way to help them learn.
alvindera97
Earned a small fee
Earned a small fee
Posts: 20
Joined: Sun Apr 12, 2020 6:11 pm

Re: Segmentation fault (core dumped) on ID value

Post by alvindera97 »

doublemax wrote: Tue Apr 14, 2020 8:03 am
catalin wrote: Tue Apr 14, 2020 6:27 am

Code: Select all

MyFrame *frame = new MyFrame();
There's your problem right there. Let's see if you understand it and then solve it.
@catalin: That's not really helpful. Even i didn't understand what you mean by that.

There are several issues with your code.

a) Change your include paths, so that you don't have to include the 'wx-3.1' part in it. The #includes need to look like this:

Code: Select all

#include <wx/wx.h>
#include <wx/textctrl.h>
#include <wx/wxprec.h>
#include <wx/button.h>
#include <wx/stattext.h>
b) The generation of the controls should happen in the MyFrame constructor. The constructor does what __init__ does in Python.

c) Under Linux even constructing a wxTextCtrl will cause an wxEVT_TEXT event. That means that the event handler "OnCourseTextBoxClicked" will be called before the member variable "course_one" is initialized. (This is the actual cause for the crash). Therefore, don't use a static event table for that event, use Bind() like for the others.

d) additionally, the variables "course_one" and "course_one_grade" have to be member variables, so that you can access them from inside the event handler

Here's the corrected code, please study the changes i made.

Code: Select all

// wxWidgets "Hello World" Program
// For compilers that support precompilation, includes "wx/wx.h".
#include <wx/wx.h>
#include <wx/textctrl.h>
#include <wx/wxprec.h>
#include <wx/button.h>
#include <wx/stattext.h>

#ifndef WX_PRECOMP
    #include <wx/wx.h>
#endif

#include <iostream>
#include <string>

//g++ hello_world.cpp `wx-config --cxxflags --libs` -o gpCalculator --verbose
//g++ hello_world.cpp `wx-config --cxxflags --libs` -o gpCalculator --verbose && ./gpCalculator

enum {
    ID_Hello = 1,
    ID_Course_list = 2,
    ID_New_Calculation = 3,
    ID_Save_Calculation = 4,
    ID_Add_Course_Button = 5,
    ID_Course_Text_Box = 6,
    ID_COURSE_ONE = 11,
    ID_COURSE_ONE_GRADE = 111,
    ID_COURSE_TWO = 12,
    ID_COURSE_TWO_GRADE = 121,
    ID_COURSE_THREE = 13,
    ID_COURSE_THREE_GRADE = 131,
    ID_COURSE_FOUR = 14,
    ID_COURSE_FOUR_GRADE = 141,
    ID_COURSE_FIVE = 15,
    ID_COURSE_FIVE_GRADE = 151,
    ID_COURSE_SIX = 16,
    ID_COURSE_SIX_GRADE = 161,
    ID_COURSE_SEVEN = 17,
    ID_COURSE_SEVEN_GRADE = 171,
    ID_COURSE_EIGHT = 18,
    ID_COURSE_EIGHT_GRADE = 181,
    ID_COURSE_NINE = 19,
    ID_COURSE_NINE_GRADE = 191,
    ID_COURSE_TEN = 110,
    ID_COURSE_TEN_GRADE = 1101,
    ID_COURSE_ELEVEN = 111,
    ID_COURSE_ELEVEN_GRADE = 1111,
    ID_COURSE_TWELVE = 12,
    ID_COURSE_TWELVE_GRADE = 1121,
};

class GpCalculator : public wxApp {
    public:
        virtual bool OnInit();

};


class MyFrame : public wxFrame {  // defines the options on the top bar of the screen here we have:
    public:
        MyFrame();
    private:
        void OnHello(wxCommandEvent& event);  // hello option
        void OnExit(wxCommandEvent& event);  // exit option
        void OnAbout(wxCommandEvent& event);  // about option
        void OnHelp(wxCommandEvent& event); // event option

        void OnCourseTextBoxClicked(wxCommandEvent &event);

        wxTextCtrl *course_one;
        wxTextCtrl *course_one_grade;

        wxDECLARE_EVENT_TABLE();
};

wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
    // EVT_TEXT(ID_COURSE_ONE, MyFrame::OnCourseTextBoxClicked)
wxEND_EVENT_TABLE()


wxIMPLEMENT_APP(GpCalculator);   // initiate main()

bool GpCalculator::OnInit() {  // append the OnInit() function
    MyFrame *frame = new MyFrame();
    frame->Show(true);
    return true;  // this is just to show successfull initialisation
}


MyFrame::MyFrame() : wxFrame(nullptr, wxID_ANY, "Engineering Grade Pending Calculator", wxPoint(100, 100), wxSize(820, 600)) {  // Frame of the program
    wxMenu *menuFile = new wxMenu;
    menuFile->AppendSeparator();
    menuFile->Append(wxID_NEW);
    menuFile->AppendSeparator();
    menuFile->Append(wxID_SAVE);
    menuFile->AppendSeparator();
    menuFile->Append(wxID_SAVEAS);
    menuFile->AppendSeparator();
    menuFile->Append(wxID_UNDO);
    menuFile->AppendSeparator();
    menuFile->Append(wxID_REDO);
    menuFile->AppendSeparator();

    menuFile->Append(wxID_EXIT);

    wxMenu *menuCourses = new wxMenu;
    menuCourses->Append(ID_Course_list, "&All Courses");
    menuCourses->AppendSeparator();
    menuCourses->Append(wxID_ANY, "&New Course");

    wxMenu *menuHelp = new wxMenu;
    menuHelp->Append(wxID_ABOUT);
    menuHelp->AppendSeparator();
    menuHelp->Append(wxID_HELP);

    wxMenuBar *menuBar = new wxMenuBar;  // create the menubar where menus would live

    // append menus to the menubar
    menuBar->Append(menuFile, "&File");  
    menuBar->Append(menuCourses, "&Course");
    menuBar->Append(menuHelp, "&Help");

    SetMenuBar( menuBar );
    CreateStatusBar();
    SetStatusText("Welcome to wxWidgets!");

// add course add buttons and list box

    // 100 Level
    wxString level_text = wxT("100 LEVEL");
    wxStaticText *st = new wxStaticText(this, wxID_ANY, level_text, wxPoint(60, 20), wxDefaultSize, wxALIGN_LEFT);
    // wxButton *add_course_button = new wxButton(this, ID_Add_Course_Button, "Add Course", wxPoint(20, 50), wxSize(120, 50));
    // wxTextCtrl *course_text_box = new wxTextCtrl(this, ID_Course_Text_Box, "", wxPoint(5, 105), wxSize(150, 30), wxTE_PROCESS_ENTER);
    // wxListBox *course_list_box = new wxListBox(this, wxID_ANY, wxPoint(5, 140), wxSize(150, 300));

    course_one = new wxTextCtrl(this, ID_COURSE_ONE, "Course 1", wxPoint(5, 44), wxSize(100, 30));
    course_one_grade = new wxTextCtrl(this, ID_COURSE_ONE_GRADE, "Grade", wxPoint(110, 44), wxSize(45, 30));

    wxTextCtrl *course_two = new wxTextCtrl(this, wxID_ANY, "Course 2", wxPoint(5, 76), wxSize(100, 30));
    wxTextCtrl *course_two_grade = new wxTextCtrl(this, wxID_ANY, "Grade", wxPoint(110, 76), wxSize(45, 30));

    wxTextCtrl *course_three = new wxTextCtrl(this, wxID_ANY, "Course 3", wxPoint(5, 108), wxSize(100, 30));
    wxTextCtrl *course_three_grade = new wxTextCtrl(this, wxID_ANY, "Grade", wxPoint(110, 108), wxSize(45, 30));

    wxTextCtrl *course_four = new wxTextCtrl(this, wxID_ANY, "Course 4", wxPoint(5, 140), wxSize(100, 30));
    wxTextCtrl *course_four_grade = new wxTextCtrl(this, wxID_ANY, "Grade", wxPoint(110, 140), wxSize(45, 30));

    wxTextCtrl *course_five = new wxTextCtrl(this, wxID_ANY, "Course 5", wxPoint(5, 172), wxSize(100, 30));
    wxTextCtrl *course_five_grade = new wxTextCtrl(this, wxID_ANY, "Grade", wxPoint(110, 172), wxSize(45, 30));

    wxTextCtrl *course_six = new wxTextCtrl(this, wxID_ANY, "Course 6", wxPoint(5, 204), wxSize(100, 30));
    wxTextCtrl *course_six_grade = new wxTextCtrl(this, wxID_ANY, "Grade", wxPoint(110, 204), wxSize(45, 30));

    wxTextCtrl *course_seven = new wxTextCtrl(this, wxID_ANY, "Course 7", wxPoint(5, 236), wxSize(100, 30));
    wxTextCtrl *course_seven_grade = new wxTextCtrl(this, wxID_ANY, "Grade", wxPoint(110, 236), wxSize(45, 30));

    wxTextCtrl *course_eight = new wxTextCtrl(this, wxID_ANY, "Course 8", wxPoint(5, 268), wxSize(100, 30));
    wxTextCtrl *course_eight_grade = new wxTextCtrl(this, wxID_ANY, "Grade", wxPoint(110, 268), wxSize(45, 30));

    wxTextCtrl *course_nine = new wxTextCtrl(this, wxID_ANY, "Course 9", wxPoint(5, 300), wxSize(100, 30));
    wxTextCtrl *course_nine_grade = new wxTextCtrl(this, wxID_ANY, "Grade", wxPoint(110, 300), wxSize(45, 30));

    wxTextCtrl *course_ten = new wxTextCtrl(this, wxID_ANY, "Course 10", wxPoint(5, 332), wxSize(100, 30));
    wxTextCtrl *course_ten_grade = new wxTextCtrl(this, wxID_ANY, "Grade", wxPoint(110, 332), wxSize(45, 30));

    wxTextCtrl *course_eleven = new wxTextCtrl(this, wxID_ANY, "Course 11", wxPoint(5, 364), wxSize(100, 30));
    wxTextCtrl *course_eleven_grade = new wxTextCtrl(this, wxID_ANY, "Grade", wxPoint(110, 364), wxSize(45, 30));

    wxTextCtrl *course_twelve = new wxTextCtrl(this, wxID_ANY, "Course 12", wxPoint(5, 396), wxSize(100, 30));
    wxTextCtrl *course_twelve_grade = new wxTextCtrl(this, wxID_ANY, "Grade", wxPoint(110, 396), wxSize(45, 30));


    wxButton *calculate_gp = new wxButton(this, wxID_ANY, "Display GP", wxPoint(42.5, 445), wxSize(75, 50));

    // 200 Level
    wxString level_text_2 = wxT("200 LEVEL");
    wxStaticText *st_2 = new wxStaticText(this, wxID_ANY, level_text_2, wxPoint(210, 20), wxDefaultSize, wxALIGN_LEFT);
    wxButton *add_course_button_2 = new wxButton(this, ID_Add_Course_Button, "Add Course", wxPoint(185, 50), wxSize(120, 50));
    wxTextCtrl *course_text_box_2 = new wxTextCtrl(this, wxID_ANY, "", wxPoint(170, 105), wxSize(150, 30), wxTE_PROCESS_ENTER);
    wxListBox *course_list_box_2 = new wxListBox(this, wxID_ANY, wxPoint(170, 140), wxSize(150, 300));
    wxButton *calculate_gp_2 = new wxButton(this, wxID_ANY, "Display GP", wxPoint(207.5, 445), wxSize(75, 50));

    // 300 Level
    wxString level_text_3 = wxT("300 LEVEL");
    wxStaticText *st_3 = new wxStaticText(this, wxID_ANY, level_text_3, wxPoint(390, 20), wxDefaultSize, wxALIGN_LEFT);
    wxButton *add_course_button_3 = new wxButton(this, ID_Add_Course_Button, "Add Course", wxPoint(350, 50), wxSize(120, 50));
    wxTextCtrl *course_text_box_3 = new wxTextCtrl(this, wxID_ANY, "", wxPoint(335, 105), wxSize(150, 30), wxTE_PROCESS_ENTER);
    wxListBox *course_list_box_3 = new wxListBox(this, wxID_ANY, wxPoint(335, 140), wxSize(150, 300));
    wxButton *calculate_gp_3 = new wxButton(this, wxID_ANY, "Display GP", wxPoint(372.5, 445), wxSize(75, 50));

    // 400 Level
    wxString level_text_4 = wxT("400 LEVEL");
    wxStaticText *st_4 = new wxStaticText(this, wxID_ANY, level_text_4, wxPoint(555, 20), wxDefaultSize, wxALIGN_LEFT);
    wxButton *add_course_button_4 = new wxButton(this, ID_Add_Course_Button, "Add Course", wxPoint(515, 50), wxSize(120, 50));
    wxTextCtrl *course_text_box_4 = new wxTextCtrl(this, wxID_ANY, "", wxPoint(500, 105), wxSize(150, 30), wxTE_PROCESS_ENTER);
    wxListBox *course_list_box_4 = new wxListBox(this, wxID_ANY, wxPoint(500, 140), wxSize(150, 300));
    wxButton *calculate_gp_4 = new wxButton(this, wxID_ANY, "Display GP", wxPoint(537.5, 445), wxSize(75, 50));

    // 500 Level
    wxString level_text_5 = wxT("500 LEVEL");
    wxStaticText *st_5 = new wxStaticText(this, wxID_ANY, level_text_5, wxPoint(720, 20), wxDefaultSize, wxALIGN_LEFT);
    wxButton *add_course_button_5 = new wxButton(this, ID_Add_Course_Button, "Add Course", wxPoint(680, 50), wxSize(120, 50));
    wxTextCtrl *course_text_box_5 = new wxTextCtrl(this, wxID_ANY, "", wxPoint(665, 105), wxSize(150, 30), wxTE_PROCESS_ENTER);
    wxListBox *course_list_box_5 = new wxListBox(this, wxID_ANY, wxPoint(665, 140), wxSize(150, 300));
    wxButton *calculate_gp_5 = new wxButton(this, wxID_ANY, "Display GP", wxPoint(702.5, 445), wxSize(75, 50));

    Bind(wxEVT_MENU, &MyFrame::OnHello, this, ID_Hello);
    Bind(wxEVT_MENU, &MyFrame::OnAbout, this, wxID_ABOUT);
    Bind(wxEVT_MENU, &MyFrame::OnExit, this, wxID_EXIT);
    Bind(wxEVT_MENU, &MyFrame::OnHelp, this, wxID_HELP);

    Bind(wxEVT_TEXT, &MyFrame::OnCourseTextBoxClicked, this, ID_COURSE_ONE);
}

void MyFrame::OnExit(wxCommandEvent& event) {
    Close(true);
}

void MyFrame::OnAbout(wxCommandEvent& event) {
    wxMessageBox("This is a programme for calculating the GP of an Engineering student at the Univesity of Benin.",
                 "GP Calculator V0.0.1", wxOK | wxICON_INFORMATION);
}

void MyFrame::OnHello(wxCommandEvent& event) {
    wxLogMessage("Hello world from wxWidgets!");
}

void MyFrame::OnHelp(wxCommandEvent& event) {
    wxMessageBox("For help on how to use this programme, please use the manual");
}

void MyFrame::OnCourseTextBoxClicked(wxCommandEvent& event)
{
    course_one->SetMaxLength(6);
    course_one_grade->SetMaxLength(1);
};
Thank you so much!!! You are a life saver! Thank you for your explanation of constructor. Your relationship to Python's __init__() was absolutely key in understanding a lot about C++ and the framework. To be honest, I've grown a very sincere liking of C++ and I really wish it was my first language. All the same thank you! I owe you.... BIG TIME. Thanks again. I'm honored. =D> =D> =D>
alvindera97
Earned a small fee
Earned a small fee
Posts: 20
Joined: Sun Apr 12, 2020 6:11 pm

Re: Segmentation fault (core dumped) on ID value

Post by alvindera97 »

catalin wrote: Tue Apr 14, 2020 8:55 am
doublemax wrote: Tue Apr 14, 2020 8:03 am
catalin wrote: Tue Apr 14, 2020 6:27 am

Code: Select all

MyFrame *frame = new MyFrame();
There's your problem right there. Let's see if you understand it and then solve it.
@catalin: That's not really helpful. Even i didn't understand what you mean by that.
Actually I was wrong there.
I meant that a frame that was not yet created was being used, but failed to realize that MyFrame() was actually calling the correct constructor (I was sure I scrolled all the way down but apparently not). Sorry for being hasty.

And...
e) User ids should be above wxID_HIGHEST.


p.s. I still think though that understanding basic feature of a language (and probably the framework too) is a decent "must" before asking people to correct 230+ lines of code. Writing the code for them, on the other hand, is most likely not the best way to help them learn.
Catalin I would also like to show my appreciation of the time and effort you put into looking into my [messy] code and contributing to a solution. I understand your concerns and pointers and I completely agree with you. You can count on me that I will definitely spend more time learning C++ properly and wxwidgets to the best of the universally accepted standards out there. Again thank you for your insights. You've all been very helpful. I don't think I would have been able to move forward you and Doublemax. Thanks again :D
Post Reply