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

wxWidgets exits before wxApp::OnInit is invoked

我的 wxApp 派生 class 在其 OnInit 被调用之前调用了它的析构函数。我想我找到了错误似乎上升的地方。在文件 appcmn.cpp 中,在方法 wxAppBase::Initialize 中,OnInitGui returns false。这似乎是由于函数返回空顶层 window。我不知道这是否真的是问题所在,而且我对发生的任何事情都一无所知。

我使用了包含在 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;
}

MainFrame.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

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

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