Hi i am using wxWidgets to create a simple app. But the example app(shown below) crashes when you click on black button on the screen. The app crashes only when you add the statement mySimplebook->GetPageCount()
from inside the onClick() event handler. If i remove the use of the above statement from inside onClick() then the app does not crash. Also the use of the above statement in the MySimplebook constructor also does not crashes the app. The program only crashes when i use mySimplebook->GetPageCount();
inside the onClick() handler. Otherwise, if you omit this statement from inside the onClick() handler the program works fine. The complete reproducible code that i have is as follows:
myframe.h
class Simple : public wxFrame
{
public:
Simple(const wxString& title);
};
myframe.cpp
Simple::Simple(const wxString& title)
: wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(650, 550))
{
MySimplebook *mainSimplebook = new MySimplebook(this, wxID_ANY);
mainSimplebook->Centre();
}
mysimplebook.h
class MySimplebook: public wxPanel
{
public:
void onClick(wxMouseEvent&);
MySimplebook(wxFrame*, int);
private:
wxSimplebook *mySimplebook;
};
mysimplebook.cpp
MySimplebook::MySimplebook(wxFrame *m_parentWindow, int id): wxPanel(m_parentWindow, id)
{
wxBoxSizer *mainBoxsizer = new wxBoxSizer(wxVERTICAL);
CustomButton *button = new CustomButton(this, wxID_ANY);
mainBoxsizer->Add(button, 1, wxEXPAND, 0);
mySimplebook = new wxSimplebook(this, wxID_ANY);
First_Page *firstPage = new First_Page(mySimplebook);
mySimplebook->AddPage(firstPage, "Input", false);
mainBoxsizer->Add(mySimplebook, wxSizerFlags(1).Expand());
/*program doesn't crashes here*/
std::cout<<"Pages inside constructor: "<<(mySimplebook->GetPageCount())<<std::endl;
this->SetSizer(mainBoxsizer);
}
void MySimplebook::onClick(wxMouseEvent &event)
{
std::cout<<"event received from button"<<std::endl;
//program creashes here
std::cout<<"pagecount inside onclick:"<<(mySimplebook->GetPageCount())<<std::endl;
}
firstpage.h
class First_Page: public wxPanel
{
public:
First_Page(wxSimplebook *);
};
firstpage.cpp
First_Page::First_Page(wxSimplebook *parent): wxPanel(parent, wxID_ANY)
{
SetBackgroundColour(wxColour(233,233,233));
}
custombutton.h
class CustomButton: public wxPanel
{
public:
CustomButton(wxWindow*, int);
};
custombutton.cpp
CustomButton::CustomButton(wxWindow *parent, int id):wxPanel(parent, id)
{
SetBackgroundColour(wxColour(0,0,0));
Connect(wxEVT_LEFT_UP, wxMouseEventHandler(MySimplebook::onClick));
}
The program crashes when i click on the button. My questions are:
- How can i resolve this runtime crash?
- Why does this crash happen? How should i avoid it in the future like using the
bind
. Or which part of my code should be changed and how?
The program crashes with the following on the console:
Pages inside constructor: 1
event received from button
Segmentation fault (core dumped)
Aucun commentaire:
Enregistrer un commentaire