Code::Blocks 16.01 with WxWidgets wxMSW 3.1.0 running on Windows 10
I am using wxSmith to create a dialog-based application and its code. When I build and run it, it crashes with a segmentation fault. I first reported this on the wxWidgets forum, and the discussion led to a solution which was to delete two extra spurious lines of code generated by wxSmith in the dialog constructor.
It crashes with a segmentation fault in wxWindowBase::InformFirstDirection(...) because when it calls GetSizer() to use as a pointer, that function is returning zero. This is line 871 in wincmn.cpp.
I reduced my wxWidgets project to a trivial case which is a wxDialog containing a wxBoxSizer (horizontal) which contains a wxPanel containing a second wxBoxSizer (vertical).
The unmodified code generated in the constructor by wxSmith is as follows:
{
wxBoxSizer* BoxSizer2;
wxBoxSizer* BoxSizer1;
Create(parent, wxID_ANY, _("Spine"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE, _T("wxID_ANY"));
BoxSizer1 = new wxBoxSizer(wxHORIZONTAL);
Panel1 = new wxPanel(this, ID_PANEL1, wxDefaultPosition, wxSize(137,566), wxTAB_TRAVERSAL, _T("ID_PANEL1"));
BoxSizer2 = new wxBoxSizer(wxVERTICAL);
Panel1->SetSizer(BoxSizer2);
SetSizer(BoxSizer2);
Layout();
BoxSizer1->Add(Panel1, 1, wxALL, 5);
SetSizer(BoxSizer1);
BoxSizer1->Fit(this); //<--- SEG FAULT IN THIS CALL
BoxSizer1->SetSizeHints(this);
}
The solution was to comment out (or remove) the two lines
SetSizer(BoxSizer2);
Layout();
(Note that SetSizer(BoxSizer2) is called twice for different windows.) The program then compiles and runs correctly. An additional problem is that each time the dialog is modified, those two lines re-appear and have to be commented out again.
No I didn't. but I have discovered what causes the incorrect lines to be generated. In the attributes for Panel1 (see my original code snippet), I don't know why, but the Width and Height were set to specific values (137,566), and Default Size was unchecked. Setting the Width and Height values back to -1 (for tidyness - makes no difference if Default Size is checked) and checking the Default Size box fixed the problem.
This is the revised code in the constructor after doing this (now builds and runs OK):
{
wxBoxSizer* BoxSizer2;
wxBoxSizer* BoxSizer1;
Create(parent, wxID_ANY, _("Spine"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE, _T("wxID_ANY"));
BoxSizer1 = new wxBoxSizer(wxHORIZONTAL);
Panel1 = new wxPanel(this, ID_PANEL1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, _T("ID_PANEL1"));
BoxSizer2 = new wxBoxSizer(wxVERTICAL);
Panel1->SetSizer(BoxSizer2);
BoxSizer2->Fit(Panel1);
BoxSizer2->SetSizeHints(Panel1);
BoxSizer1->Add(Panel1, 1, wxALL, 5);
SetSizer(BoxSizer1);
BoxSizer1->Fit(this);
BoxSizer1->SetSizeHints(this);
}
There is probably still a bug in wxSmith code generation somewhere, which causes the original problem.