Associate the window with the sizer in wxSizer::Replace()

If wxSizerItem passed to Replace() contains a window, the window must be
associated with the sizer to ensure that it is uncoupled from it when it
is destroyed.

Add a simple test which resulted in a use-after-free before but passes
now.
This commit is contained in:
Vadim Zeitlin
2020-07-05 23:19:36 +02:00
parent 2f49325d4c
commit 957183ef47
2 changed files with 9 additions and 0 deletions

View File

@@ -883,6 +883,9 @@ bool wxSizer::Replace( size_t old, wxSizerItem *newitem )
delete item; delete item;
if (wxWindow* const w = newitem->GetWindow())
w->SetContainingSizer(this);
return true; return true;
} }

View File

@@ -444,3 +444,9 @@ TEST_CASE_METHOD(BoxSizerTestCase, "BoxSizer::IncompatibleFlags", "[sizer]")
#undef ASSERT_SIZER_INCOMPATIBLE_FLAGS #undef ASSERT_SIZER_INCOMPATIBLE_FLAGS
#undef ASSERT_SIZER_INVALID_FLAGS #undef ASSERT_SIZER_INVALID_FLAGS
} }
TEST_CASE_METHOD(BoxSizerTestCase, "BoxSizer::Replace", "[sizer]")
{
m_sizer->AddSpacer(1);
m_sizer->Replace(0, new wxSizerItem(new wxWindow(m_win, wxID_ANY)));
}