From 23927957e118d63460f645a27e90086c24f9a420 Mon Sep 17 00:00:00 2001 From: jean-pierre charras Date: Wed, 13 May 2020 11:51:56 +0200 Subject: [PATCH] Pcbnew: fix a crash due to use of invalid pointers after a list modification Fixes #4409 https://gitlab.com/kicad/code/kicad/issues/4409 --- .../dialog_edit_footprint_for_fp_editor.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/pcbnew/dialogs/dialog_edit_footprint_for_fp_editor.cpp b/pcbnew/dialogs/dialog_edit_footprint_for_fp_editor.cpp index 51c0c78a7f..690c942e9a 100644 --- a/pcbnew/dialogs/dialog_edit_footprint_for_fp_editor.cpp +++ b/pcbnew/dialogs/dialog_edit_footprint_for_fp_editor.cpp @@ -607,20 +607,27 @@ bool DIALOG_FOOTPRINT_FP_EDITOR::TransferDataFromWindow() m_footprint->Value() = m_texts->at( 1 ); size_t i = 2; - for( auto item : m_footprint->GraphicalItems() ) + std::vector items_to_remove; + + for( BOARD_ITEM* item : m_footprint->GraphicalItems() ) { - TEXTE_MODULE* textModule = dyn_cast( item ); + TEXTE_MODULE* textModule = dynamic_cast( item ); if( textModule ) { // copy grid table entries till we run out, then delete any remaining texts if( i < m_texts->size() ) *textModule = m_texts->at( i++ ); - else - textModule->DeleteStructure(); + else // store this item to remove and delete it later, + // after the graphic list is explored: + items_to_remove.push_back( textModule ); } } + // Remove items from m_footprint graphic list: + for( TEXTE_MODULE* item: items_to_remove ) + item->DeleteStructure(); + // if there are still grid table entries, create new texts for them while( i < m_texts->size() ) {