wxSizer causing crash on exit

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Rob190-2
In need of some credit
In need of some credit
Posts: 6
Joined: Fri Nov 12, 2021 3:41 pm

wxSizer causing crash on exit

Post by Rob190-2 »

I've been using sizers for many years but in trying to debug a rather persistent bug that's cause a crash when the app exits, I realized I didn't understand some of the usage rules.

The layout I'm trying to achieve is:

Code: Select all

//   ------------------------------------------------------------------------------
//  |             sizer_frame->sizer_frame_upper->sizer_panel_top_upper            |
//  |------------------------------------------------------------------------------|
//  |             sizer_frame->sizer_frame_upper->sizer_panel_top_lower            |
//  |------------------------------------------------------------------------------|
//  |                                                        |                     |
//  |                                                        |                     |
//  | sizer_frame->sizer_frame_lower->sizer_frame_lower_left |     canvas          |
//  |                                                        |                     |
//  |                                                        |                     |
//  |                                                        |                     |
//  |                                                        |                     |
//   ------------------------------------------------------------------------------
A subset of these sizer code is below. The question I have is when adding wxPanels to the layout, why adding the panel seems to work but adding its wxSizer causes it to crash

Code: Select all

  // wxFrame constructor
  
  sizer_frame = new wxBoxSizer (wxVERTICAL);
  SetSizer (sizer_frame);
  
  panel_top = new wxPanel (this);
  sizer_frame_upper = new wxBoxSizer (wxVERTICAL);
  panel_top->SetSizer (sizer_frame_upper);

  sizer_frame_lower = new wxBoxSizer (wxHORIZONTAL);

  canvas = new My_Canvas (this);

  //sizer_frame->Add (sizer_frame_upper, 0, wxEXPAND); -- this crashes on exit
  sizer_frame->Add (panel_top, 0, wxEXPAND); // Using this in place of the above doesn't crash

  sizer_frame->Add (sizer_frame_lower, 1, wxEXPAND);

  sizer_frame_lower->Add (canvas, 1, wxEXPAND);
The crash is occurring in wxSizerItem::Free() but all the user code is gone by the time it happens so I haven't been able to find which object is causing the problem.

In debugging this, I'm assuming it's ok to have empty sizers. Also it's not clear to me if wxSizer::Layout() needs to be called explicitly or whether this is done by the framework.

Rob.
User avatar
doublemax
Moderator
Moderator
Posts: 19159
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxSizer causing crash on exit

Post by doublemax »

A sizer can't be assigned to a window panel_top->SetSizer (sizer_frame_upper); and inserted into another sizer sizer_frame->Add (sizer_frame_upper, 0, wxEXPAND); at the same time.

If panel_top is destroyed, it will destroy the sizer.
If sizer_frame is destroyed, it will also destroy the sizer.
Use the source, Luke!
Rob190-2
In need of some credit
In need of some credit
Posts: 6
Joined: Fri Nov 12, 2021 3:41 pm

Re: wxSizer causing crash on exit

Post by Rob190-2 »

Thanks. That makes sense.

Could a runtime check be added to detect this case? I'm a great fan of sizers but I find they are one of the most difficult things to get working. I've lost count of the number of times I've had to scrap large chunks of code and start over.

Rob.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7477
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxSizer causing crash on exit

Post by ONEEYEMAN »

Hi,
Are you writing the sizer code by hand?
If you want to -send the e-mail to wx-users ML and ask for such check. This is a user forum - made by users for users. No wx-dev core are coming here.

Thank yiou.
Post Reply