我有一个简单的(单个对话框)wxWidgets应用程序,到目前为止,该应用程序运行良好,但是由于我在该对话框中添加了wxTextCtrl,因此每次程序退出时它都会出现段错误。

我猜想肯定有一个显而易见的错误,因为samples/text程序包含许多文本控件并且可以正常运行。

该问题仅在删除时出现,即通常仅在退出程序时出现(因此从理论上讲,我可以通过使用wxTextCtrl退出而无需清理来解决该问题,但我真的不想这样做。) segfault发生在属于文本控件的exitwxColour的析构函数中。

同样,只有在为mingw32编译时才会发生。 MSVC和所有64位版本均不受影响。

wxWidgets版本是2.8.12。我尚未尝试使用2.9.x。

这是一个说明问题的最小程序。它所做的只是创建一个填充主窗口的多行文本控件:

#include "wx/wx.h"

enum
{
    Minimal_Quit = wxID_EXIT,
};

class MyApp : public wxApp
{
public:
    MyApp() {}
    virtual bool OnInit();
    virtual int OnExit();

private:
    DECLARE_EVENT_TABLE()
};


class MyFrame : public wxFrame
{
public:
    MyFrame(const wxString &title);
    virtual ~MyFrame();
    void OnQuit(wxCommandEvent &event);

private:
    DECLARE_EVENT_TABLE()
};

BEGIN_EVENT_TABLE(MyFrame, wxFrame)
    EVT_MENU(Minimal_Quit,  MyFrame::OnQuit)
END_EVENT_TABLE()

bool MyApp::OnInit()
{
    if (! wxApp::OnInit()) return false;
    MyFrame *frame = new MyFrame(_T("Test"));
    frame->Show(true);
    return true;
}

int MyApp::OnExit()
{
    return 0;
}

IMPLEMENT_APP(MyApp)

BEGIN_EVENT_TABLE(MyApp, wxApp)
END_EVENT_TABLE()

MyFrame::MyFrame(const wxString& title)
       : wxFrame(NULL, wxID_ANY, title)
{
  wxBoxSizer *sizermain = new wxBoxSizer(wxVERTICAL);
  sizermain->SetMinSize(640, 400);
  wxTextCtrl *textCtrl =
    new wxTextCtrl(this, wxID_ANY, _T("Hello, world"), wxDefaultPosition, wxDefa
ultSize, wxTE_MULTILINE);
  sizermain->Add(textCtrl, 1, wxALL | wxEXPAND);
  this->SetSizer(sizermain);
  sizermain->SetSizeHints(this);
}

MyFrame::~MyFrame()
{
}

void MyFrame::OnQuit(wxCommandEvent &WXUNUSED(event))
{
    Close(true);
}


这是堆栈跟踪:

Program received signal SIGSEGV, Segmentation fault.
0x6338155c in wxObject::UnRef (this=0x755d37c) at ./src/common/object.cpp:348
348             if ( --m_refData->m_count == 0 )
(gdb) where
#0  0x6338155c in wxObject::UnRef (this=0x755d37c)
    at ./src/common/object.cpp:348
#1  0x006602a3 in wxObject::~wxObject (this=0x755d37c,
    __in_chrg=<optimized out>) at ./include/wx/object.h:413
#2  0x00638d34 in wxGDIObject::~wxGDIObject (this=0x755d37c,
    __in_chrg=<optimized out>) at ./include/wx/gdiobj.h:29
#3  0x0063bc60 in wxColourBase::~wxColourBase (this=0x755d37c,
    __in_chrg=<optimized out>) at ./include/wx/colour.h:64
#4  0x0049c094 in wxColour::~wxColour (this=0x755d37c,
    __in_chrg=<optimized out>) at ./src/msw/colour.cpp:77
#5  0x0063784d in wxTextAttr::~wxTextAttr (this=0x755d364,
    __in_chrg=<optimized out>) at ./include/wx/textctrl.h:189
#6  0x006456be in wxTextCtrlBase::~wxTextCtrlBase (this=0x755d210,
    __in_chrg=<optimized out>) at ./include/wx/textctrl.h:295
#7  0x0050f8ef in wxTextCtrl::~wxTextCtrl (this=0x755d210,
    __in_chrg=<optimized out>) at ./src/msw/textctrl.cpp:290
#8  0x0050f937 in wxTextCtrl::~wxTextCtrl (this=0x755d210,
    __in_chrg=<optimized out>) at ./src/msw/textctrl.cpp:293
#9  0x005888cf in wxWindowBase::DestroyChildren (this=0x755c878)
    at ./src/common/wincmn.cpp:447
#10 0x004c311a in wxWindow::~wxWindow (this=0x755c878,
    __in_chrg=<optimized out>) at ./src/msw/window.cpp:561
#11 0x00583e0b in wxTopLevelWindowBase::~wxTopLevelWindowBase (
    this=0x755c878, __in_chrg=<optimized out>) at ./src/common/toplvcmn.cpp:62
#12 0x004bf18b in wxTopLevelWindowMSW::~wxTopLevelWindowMSW (this=0x755c878,
    __in_chrg=<optimized out>) at ./src/msw/toplevel.cpp:618
#13 0x0064be2c in wxTopLevelWindow::~wxTopLevelWindow (this=0x755c878,
    __in_chrg=<optimized out>) at ./include/wx/toplevel.h:352
#14 0x005434ed in wxFrameBase::~wxFrameBase (this=0x755c878,
    __in_chrg=<optimized out>) at ./src/common/framecmn.cpp:76
#15 0x004e9bc5 in wxFrame::~wxFrame (this=0x755c878,
    __in_chrg=<optimized out>) at ./src/msw/frame.cpp:210
#16 0x00401730 in MyFrame::~MyFrame (this=0x755c878,
    __in_chrg=<optimized out>) at MainFrame.cpp:63
#17 0x0040175d in MyFrame::~MyFrame (this=0x755c878,
    __in_chrg=<optimized out>) at MainFrame.cpp:65
#18 0x0051fabf in wxAppBase::DeletePendingObjects (this=0x7551240)
    at ./src/common/appcmn.cpp:423
#19 0x0051fc09 in wxAppBase::ProcessIdle (this=0x7551240)
    at ./src/common/appcmn.cpp:454
#20 0x0053dbe0 in wxEventLoopManual::Run (this=0x755e1b0)
    at ./src/common/evtloopcmn.cpp:99
#21 0x0051f7f7 in wxAppBase::MainLoop (this=0x7551240)
    at ./src/common/appcmn.cpp:312
#22 0x0051f92d in wxAppBase::OnRun (this=0x7551240)
    at ./src/common/appcmn.cpp:367
#23 0x633709fc in wxEntryReal (argc=@0x22fe5c: 1, argv=0x3e81b8)
    at ./src/common/init.cpp:448
#24 0x633c09bc in wxEntry (argc=@0x22fe5c: 1, argv=0x3e81b8)
    at ./src/msw/main.cpp:231
#25 0x004951f3 in wxEntry (hInstance=0x400000, nCmdShow=10)
    at ./src/msw/main.cpp:386
#26 0x0040150c in WinMain@16 (hInstance=0x400000, hPrevInstance=0x0,
    lpCmdLine=0x241f19 "", nCmdShow=10) at MainFrame.cpp:48
#27 0x00403cdb in main ()

最佳答案

该代码是完全正确的,并且绝对可以工作(在任何版本的wxWidgets中),因此,我看到的唯一解释是您遇到某种形式的错误编译问题,即,也许您是针对与构建库或使用不同(且不兼容)的编译器选项。解决此类问题的最简单方法是删除所有内容并重建。还要确保您的系统上只有一组头文件/库。

关于c++ - 在简单的wxWidgets应用中退出时出现段错误(无论何时使用文本控件),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19800460/

10-14 13:22