From 19a9cac436cc896bc1d087bd42bfb68d0ed2faa8 Mon Sep 17 00:00:00 2001 From: Seth Hillbrand Date: Mon, 29 Dec 2025 08:37:48 -0800 Subject: [PATCH] Prevent holding dangling pointers when rebuilding Changing properties consecutively can rebuild the grid, leading to dangling pointers. Storing references by name keeps the current pointer accurate Fixes https://gitlab.com/kicad/code/kicad/-/issues/22262 (cherry picked from commit 458db8864820c699aeff17179b37db7970f40770) --- common/properties/pg_editors.cpp | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/common/properties/pg_editors.cpp b/common/properties/pg_editors.cpp index ab5568b29a..e899e9b75c 100644 --- a/common/properties/pg_editors.cpp +++ b/common/properties/pg_editors.cpp @@ -311,13 +311,21 @@ wxPGWindowList PG_COLOR_EDITOR::CreateControls( wxPropertyGrid* aGrid, wxPGPrope editor->SetPosition( aPos ); editor->SetSize( aSize ); + // Capture property name instead of pointer to avoid dangling pointer if grid is rebuilt + wxString propName = colorProp->GetName(); + editor->Bind( COLOR_SWATCH_CHANGED, [=]( wxCommandEvent& aEvt ) { - wxVariant val; - auto data = new COLOR4D_VARIANT_DATA( editor->GetSwatchColor() ); - val.SetData( data ); - aGrid->ChangePropertyValue( colorProp, val ); + wxPGProperty* prop = aGrid->GetPropertyByName( propName ); + + if( prop ) + { + wxVariant val; + auto data = new COLOR4D_VARIANT_DATA( editor->GetSwatchColor() ); + val.SetData( data ); + aGrid->ChangePropertyValue( prop, val ); + } } ); #if wxCHECK_VERSION( 3, 3, 0 ) @@ -330,6 +338,11 @@ wxPGWindowList PG_COLOR_EDITOR::CreateControls( wxPropertyGrid* aGrid, wxPGPrope [=]() { editor->GetNewSwatchColor(); + + wxPGProperty* prop = aGrid->GetPropertyByName( propName ); + + if( prop ) + aGrid->DrawItem( prop ); } ); }