I wrote a simple wxWidgets program and it crashes when the program is closed:
Code: Select all
// headers
#include "wx/wx.h"
#include <boost/smart_ptr.hpp>
using boost::shared_ptr;
// IDs for the controls and the menu commands
enum
{
// menu items
Minimal_Quit = 1,
Minimal_About
};
// Define a new frame type: this is going to be our main frame
class MyFrame : public wxFrame
{
public:
// ctor(s)
MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size,
long style = wxDEFAULT_FRAME_STYLE) : wxFrame(NULL, -1, title, pos, size, style),
fileMenu(new wxMenu),
helpMenu(new wxMenu),
menuBar(new wxMenuBar)
{
// the "About" item should be in the help menu
helpMenu->Append(Minimal_About, "&About...\tF1", "Show about dialog");
fileMenu->Append(Minimal_Quit, "E&xit\tAlt-X", "Quit this program");
// now append the freshly created menu to the menu bar...
menuBar->Append(fileMenu.get(), "&File");
menuBar->Append(helpMenu.get(), "&Help");
// ... and attach this menu bar to the frame
SetMenuBar(menuBar.get());
// create a status bar just for fun (by default with 1 pane only)
CreateStatusBar();
SetStatusText("Welcome to wxWindows!");
}
// event handlers (these functions should _not_ be virtual)
void OnQuit(wxCommandEvent& event)
{
// TRUE is to force the frame to close
Close(true);
}
void OnAbout(wxCommandEvent& event)
{
wxString msg;
msg.Printf("This is the About dialog of the minimal sample.\nWelcome to %s", wxVERSION_STRING);
wxMessageBox(msg, "About Minimal", wxOK | wxICON_INFORMATION, this);
}
private:
// data members
shared_ptr<wxMenu> fileMenu;
shared_ptr<wxMenu> helpMenu;
shared_ptr<wxMenuBar> menuBar;
// any class wishing to process wxWindows events must use this macro
DECLARE_EVENT_TABLE()
};
// Define a new application type, each program should derive a class from wxApp
class MyApp : public wxApp
{
public:
// cosntructor
MyApp() : frame(new MyFrame("Minimal wxWindows App", wxPoint(50, 50), wxSize(450, 340))) {}
// override base class virtuals
// ----------------------------
// this one is called on application startup and is a good place for the app
// initialization (doing it here and not in the ctor allows to have an error
// return: if OnInit() returns false, the application terminates)
// 'Main program' equivalent: the program execution "starts" here
virtual bool OnInit()
{
// show the main application window (the frames, unlike simple controls, are not shown when
// created initially)
frame->Show(true);
// success: wxApp::OnRun() will be called which will enter the main message
// loop and the application will run. If we returned FALSE here, the
// application would exit immediately.
return true;
}
private:
// data members
shared_ptr<MyFrame> frame;
};
// ----------------------------------------------------------------------------
// event tables and other macros for wxWindows
// ----------------------------------------------------------------------------
// the event tables connect the wxWindows events with the functions (event
// handlers) which process them. It can be also done at run-time, but for the
// simple menu events like this the static method is much simpler.
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(Minimal_Quit, MyFrame::OnQuit)
EVT_MENU(Minimal_About, MyFrame::OnAbout)
END_EVENT_TABLE()
// Create a new application object: this macro will allow wxWindows to create
// the application object during program execution (it's better than using a
// static object for many reasons) and also declares the accessor function
// wxGetApp() which will return the reference of the right type (i.e. MyApp and
// not wxApp)
IMPLEMENT_APP(MyApp)