diff --git a/pcbnew/pcb_io/kicad_sexpr/pcb_io_kicad_sexpr_parser.cpp b/pcbnew/pcb_io/kicad_sexpr/pcb_io_kicad_sexpr_parser.cpp index 5b578b4c13..bbee91cbb0 100644 --- a/pcbnew/pcb_io/kicad_sexpr/pcb_io_kicad_sexpr_parser.cpp +++ b/pcbnew/pcb_io/kicad_sexpr/pcb_io_kicad_sexpr_parser.cpp @@ -4750,20 +4750,34 @@ FOOTPRINT* PCB_IO_KICAD_SEXPR_PARSER::parseFOOTPRINT_unchecked( wxArrayString* a { // Fields other than reference and value weren't historically // stored in fp_texts so we don't need to handle them here + bool text_must_be_deleted = false; + switch( field->GetId() ) { case REFERENCE_FIELD: footprint->Reference() = PCB_FIELD( *text, REFERENCE_FIELD ); const_cast( footprint->Reference().m_Uuid ) = text->m_Uuid; - delete text; + text_must_be_deleted = true; break; case VALUE_FIELD: footprint->Value() = PCB_FIELD( *text, VALUE_FIELD ); const_cast( footprint->Value().m_Uuid ) = text->m_Uuid; - delete text; + text_must_be_deleted = true; break; } + + if( text_must_be_deleted ) + { + // We don't want to leave a dangling pointer in the map + if( auto it = m_fontTextMap.find( text ); it != m_fontTextMap.end() ) + { + // TODO: try to keep the font specified in m_fontTextMap + m_fontTextMap.erase( it ); + } + + delete text; + } } else footprint->Add( text, ADD_MODE::APPEND, true );