Author Topic: WXSmith generates invalid code  (Read 2818 times)

Offline JeffG1

  • Single posting newcomer
  • *
  • Posts: 3
WXSmith generates invalid code
« on: August 03, 2017, 05:08:27 pm »
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:

Code
{
    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
Code
    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.

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 3353
Re: WXSmith generates invalid code
« Reply #1 on: August 03, 2017, 06:15:49 pm »
Can you try a nightly build? I think this was fixed there (at least there was a discussion about wrong wx3.xx generated code).

Offline JeffG1

  • Single posting newcomer
  • *
  • Posts: 3
Re: WXSmith generates invalid code
« Reply #2 on: August 04, 2017, 12:11:14 pm »
Oh right - thanks. I will try that and get back.

EDIT: Downloaded and ran the nightly build, and those two lines are still there, causing the SIGSEGV.

PS: Is there any chance that one day wxFormBuilder might be integrated into Code::Blocks in the same way as wxSmith? (OK on search I found others are discussing wxFormBuilder already.)
« Last Edit: August 04, 2017, 12:44:28 pm by JeffG1 »

Online Commaster

  • Almost regular
  • **
  • Posts: 171
Re: WXSmith generates invalid code
« Reply #3 on: August 04, 2017, 02:02:08 pm »
When you say, you ran the nightly build, have you restarted the project from scratch and recreated the form?

Offline JeffG1

  • Single posting newcomer
  • *
  • Posts: 3
Re: WXSmith generates invalid code
« Reply #4 on: August 06, 2017, 11:25:20 am »
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):

Code
{
    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.