Fix incorrect use of iterators.

Iterators always create problems when using them on a list that is modified during iteration.
This commit is contained in:
jean-pierre charras 2017-07-04 12:47:13 +02:00
parent 969209c2cc
commit 1724f902a1
1 changed files with 21 additions and 13 deletions

View File

@ -160,14 +160,15 @@ void PCB_EDIT_FRAME::Delete_Drawings_All_Layer( PCB_LAYER_ID aLayer )
if( !IsOK( this, msg ) ) if( !IsOK( this, msg ) )
return; return;
PICKED_ITEMS_LIST pickList; // Step 1: build the list of items to remove.
ITEM_PICKER picker( NULL, UR_DELETED ); // because we are using iterators, we cannot modify the drawing list during iterate
BOARD_ITEM* PtNext; // so we are using a 2 steps calculation:
// First, collect items.
// Second, remove items.
std::vector<BOARD_ITEM*> list;
for( auto item : GetBoard()->Drawings() ) for( auto item : GetBoard()->Drawings() )
{ {
PtNext = item->Next();
switch( item->Type() ) switch( item->Type() )
{ {
case PCB_LINE_T: case PCB_LINE_T:
@ -175,11 +176,7 @@ void PCB_EDIT_FRAME::Delete_Drawings_All_Layer( PCB_LAYER_ID aLayer )
case PCB_DIMENSION_T: case PCB_DIMENSION_T:
case PCB_TARGET_T: case PCB_TARGET_T:
if( item->GetLayer() == aLayer ) if( item->GetLayer() == aLayer )
{ list.push_back( item );
item->UnLink();
picker.SetItem( item );
pickList.PushItem( picker );
}
break; break;
@ -193,11 +190,22 @@ void PCB_EDIT_FRAME::Delete_Drawings_All_Layer( PCB_LAYER_ID aLayer )
} }
} }
if( pickList.GetCount() ) if( list.size() == 0 ) // No item found
return;
// Step 2: remove items from main list, and move them to the undo list
PICKED_ITEMS_LIST pickList;
ITEM_PICKER picker( NULL, UR_DELETED );
for( auto item : list )
{ {
OnModify(); item->UnLink();
SaveCopyInUndoList(pickList, UR_DELETED); picker.SetItem( item );
pickList.PushItem( picker );
} }
OnModify();
SaveCopyInUndoList(pickList, UR_DELETED);
} }