wxWidgets 在调用 wxApp::OnInit 之前退出

wxWidgets exits before wxApp::OnInit is invoked

本文关键字:退出 OnInit wxWidgets 调用 wxApp      更新时间:2023-10-16

我的wxApp派生类在调用其OnInit之前调用了其析构函数。我想我追踪到了错误似乎上升的地方。在文件 appcmn.cpp 中,在方法 wxAppBase::Initialize 中,OnInitGui 返回 false。这似乎是由于函数返回一个空的顶级窗口。我不知道这是否真的是问题所在,无论哪种方式,我都对正在发生的事情一无所知。

  • 操作系统: 视窗 7 家庭 64 位
  • 编译器:来自Visual Studio 2013 Express的cl 64位
  • wx小部件版本: 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

虚空工匠应用程序.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,除非被覆盖,并且您似乎没有覆盖它,因此看起来您可能有某种糟糕的构建,调用了错误的虚拟函数(如果您使用较新版本的标头和较旧的库或类似的东西,则可能会发生这种情况)。所以我的建议是从头开始重建所有内容,即库和应用程序,看看它是否能解决问题。

如果没有,请考虑运行与您的程序非常相似的最小示例。如果它不起作用,问题肯定出在构建上。如果是这样,那么,通过将其与代码之间的差异一分为二,您应该能够快速找到导致程序失败的关键更改。