【问题标题】:wxWidgets exits before wxApp::OnInit is invokedwxWidgets 在调用 wxApp::OnInit 之前退出
【发布时间】:2015-01-05 06:22:15
【问题描述】:

我的 wxApp 派生类在调用 OnInit 之前调用了它的析构函数。我想我找到了错误似乎上升的地方。在文件 appcmn.cpp 中,在方法 wxAppBase::Initialize 中,OnInitGui 返回 false。这似乎是由于函数返回了一个空的顶级窗口。不过,我不知道这是否真的是问题所在,无论发生什么,我都有些不知所措。

  • 操作系统:Windows 7 家庭 64 位
  • 编译器:来自 Visual Studio 2013 Express 的 cl 64bit
  • 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;
}

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);
}

【问题讨论】:

    标签: c++ wxwidgets


    【解决方案1】:

    OnInitGui() 在 wxMSW 中总是返回 true,除非被覆盖,而且你似乎没有覆盖它,所以看起来你可能有某种错误的构建,调用了错误的虚函数(这可能发生如果您使用较新版本的标头和较旧的库或类似的东西)。所以我的建议是从头开始重建所有内容,即库和您的应用程序,看看它是否能解决问题。

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

    【讨论】:

    • 就是这样!我注意到关于示例程序的 wxWidgets 有效,并且看到该项目引用了位于构建库目录 wxWidgetsBaseDir/lib/vc_x64_lib/mswud 的头文件 wx/setup.h 和 wx/msw/rcdefs.h。将这些标题复制到我自己项目的 wxWidgets 包含目录,现在我的程序可以工作了。非常感谢!
    猜你喜欢
    • 2021-02-12
    • 1970-01-01
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多