Question

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 ()
Was it helpful?

Solution

The code is perfectly correct and definitely works (in any version of wxWidgets) so the only explanation I see is that you have some sort of miscompilation issue, i.e. perhaps you compiled your application against different headers than those that were used when building the library or using different (and incompatible) compiler options. The easiest way to deal with this kind of issue is to delete everything and rebuild. Also make sure that you have only a single set of headers/libraries on your system.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top