我的 wxApp 派生类在其 OnInit 被调用之前调用了它的析构函数。我想我找到了错误似乎上升的地方。在文件 appcmn.cpp 中,在方法 wxAppBase::Initialize 中,OnInitGui 返回 false。这似乎是由于函数返回一个空的顶级窗口。我不知道这是否真的是问题所在,而且我对发生的事情一无所知。
- 操作系统:Windows 7 家庭版 64 位
- 编译器:来自 Visual Studio 2013 Express 的 cl 64 位
- wxWidgets 版本:3.0.2
我使用 wxWidgets 下载中包含的 Visual Studio 2013 Express 解决方案来构建 64 位调试 wxWidgets 库。
这是我的代码。
VoidCrafterApp.h
#ifndef VOIDCRAFTERAPP_H
#define VOIDCRAFTERAPP_H
#include "wx/wx.h"
class MainFrame;
class VoidCrafterApp : public wxApp
{
MainFrame* m_pMainframe;
public:
~VoidCrafterApp();
virtual bool OnInit();
};
DECLARE_APP(VoidCrafterApp);
#endif
VoidCrafterApp.cpp
#include "VoidCrafterApp.h"
#include "MainFrame.h"
IMPLEMENT_APP(VoidCrafterApp);
VoidCrafterApp::~VoidCrafterApp()
{
bool wegothere = true; // This statement is executed
}
bool VoidCrafterApp::OnInit()
{
m_pMainframe = new MainFrame("Hello World", wxPoint(50, 50), wxSize(450, 340));
m_pMainframe->Show( true );
return true;
}
主框架.h
#ifndef MAINFRAME_H
#define MAINFRAME_H
#include "wx/frame.h"
#include "wx/menu.h"
enum
{
ID_Hello = 1
};
class MainFrame : public wxFrame
{
// Top Menu Bar
wxMenuBar* m_pMenuBar;
public:
MainFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
private:
void OnHello(wxCommandEvent& event);
void OnExit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
wxDECLARE_EVENT_TABLE();
};
#endif
主框架.cpp
#include "MainFrame.h"
#include "wx/wx.h"
wxBEGIN_EVENT_TABLE(MainFrame, wxFrame)
EVT_MENU(ID_Hello, MainFrame::OnHello)
EVT_MENU(wxID_EXIT, MainFrame::OnExit)
EVT_MENU(wxID_ABOUT, MainFrame::OnAbout)
wxEND_EVENT_TABLE()
MainFrame::MainFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame(NULL, wxID_ANY, title, pos, size)
{
// Create the top menus
wxMenu* pMenuFile = new wxMenu();
pMenuFile->Append(ID_Hello, "&Hello...\tCtrl-H",
"Help string shown in status bar for this menu item");
pMenuFile->AppendSeparator();
pMenuFile->Append(wxID_EXIT);
wxMenu* pMenuHelp = new wxMenu();
pMenuHelp->Append(wxID_ABOUT);
wxMenuBar *m_pMenuBar = new wxMenuBar;
m_pMenuBar->Append(pMenuFile, "&File");
m_pMenuBar->Append(pMenuHelp, "&Help");
SetMenuBar(m_pMenuBar);
CreateStatusBar();
SetStatusText("Welcome to wxWidgets!");
}
void MainFrame::OnHello(wxCommandEvent& event)
{
wxLogMessage("Hello world from wxWidgets!");
}
void MainFrame::OnExit(wxCommandEvent& event)
{
Close(false);
}
void MainFrame::OnAbout(wxCommandEvent& event)
{
wxMessageBox("This is a wxWidgets' Hello world sample",
"About Hello World", wxOK | wxICON_INFORMATION);
}
最佳答案
OnInitGui()
在 wxMSW 中总是返回 true,除非被覆盖,而且你似乎没有覆盖它,所以看起来你可能有某种错误的构建,错误的虚拟被调用的函数(如果您将来自较新版本的 header 与较旧的库或类似的东西一起使用,则会发生这种情况)。因此,我的建议是从头开始重建所有内容,即库和您的应用程序,看看是否能解决问题。
如果没有,请考虑运行与您的程序非常相似的最小样本。如果它不起作用,问题肯定出在构建上。如果确实如此,那么通过平分它与您的代码之间的差异,您应该能够快速找到导致程序失败的关键更改。
关于c++ - wxWidgets 在调用 wxApp::OnInit 之前退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27774375/