0

I have a simple (single dialog) wxWidgets application which has worked well so far, but since I added a wxTextCtrl to the dialog it now segfaults every time the program exits.

I'm guessing there must be an obvious bug that I am missing, as the samples/text program contains many text controls and works fine.

The problem occurs only when deleting a wxTextCtrl, i.e. usually only when quitting the program (so in theory I could work around it by using exit to quit without cleanup, but I don't really want to do that.) The segfault occurs in the destructor of a wxColour that belongs to a wxTextAttr that belongs to the text control.

Also it only happens when compiled for mingw32. MSVC and all 64-bit builds are unaffected.

The wxWidgets version is 2.8.12. I have not tried with 2.9.x yet.

Here is a minimal program that illustrates the problem. All it does is create one multiline text control that fills the main window:

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

Here is the stack trace:

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 ()
4

1 回答 1

2

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

于 2013-11-05T23:54:18.173 回答