diff --git a/eeschema/files-io.cpp b/eeschema/files-io.cpp index fefefdc5a7..2f79b92c52 100644 --- a/eeschema/files-io.cpp +++ b/eeschema/files-io.cpp @@ -146,6 +146,7 @@ bool SCH_EDIT_FRAME::OpenProjectFiles( const std::vector& aFileSet, in // unload current project file before loading new { ClearUndoRedoList(); + ClearRepeatItemsList(); SetScreen( nullptr ); m_toolManager->GetTool()->Reset( TOOL_BASE::SUPERMODEL_RELOAD ); CreateScreens(); @@ -742,6 +743,7 @@ void SCH_EDIT_FRAME::OnImportProject( wxCommandEvent& aEvent ) // Don't leave dangling pointers to previously-opened document. m_toolManager->GetTool()->ClearSelection(); ClearUndoRedoList(); + ClearRepeatItemsList(); if( setProject ) { @@ -1462,6 +1464,7 @@ bool SCH_EDIT_FRAME::importFile( const wxString& aFileName, int aFileType, } ClearUndoRedoList(); + ClearRepeatItemsList(); initScreenZoom(); SetSheetNumberAndCount(); diff --git a/eeschema/sch_edit_frame.cpp b/eeschema/sch_edit_frame.cpp index bee7019e10..bdce560bba 100644 --- a/eeschema/sch_edit_frame.cpp +++ b/eeschema/sch_edit_frame.cpp @@ -404,6 +404,11 @@ SCH_EDIT_FRAME::~SCH_EDIT_FRAME() if( m_schematic ) m_schematic->RemoveAllListeners(); + // Delete all items not in draw list before deleting schematic + // to avoid dangling pointers stored in these items + ClearUndoRedoList(); + ClearRepeatItemsList(); + delete m_schematic; m_schematic = nullptr; diff --git a/eeschema/sch_edit_frame.h b/eeschema/sch_edit_frame.h index 00a7564c4b..be28f6d205 100644 --- a/eeschema/sch_edit_frame.h +++ b/eeschema/sch_edit_frame.h @@ -710,6 +710,15 @@ public: return m_items_to_repeat; } + /** + * Clear the list of items which are to be repeated with the insert key. + * These objects are owned by this container. + */ + void ClearRepeatItemsList() + { + m_items_to_repeat.clear(); + } + EDA_ITEM* GetItem( const KIID& aId ) const override; /** diff --git a/eeschema/sch_item.cpp b/eeschema/sch_item.cpp index fb937b0e82..525b3b9b63 100644 --- a/eeschema/sch_item.cpp +++ b/eeschema/sch_item.cpp @@ -90,6 +90,11 @@ SCH_ITEM::~SCH_ITEM() for( const auto& it : m_connection_map ) delete it.second; + // Do not try to modify SCHEMATIC::ConnectionGraph() + // if the schematic does not exist + if( !SCHEMATIC::m_IsSchematicExists ) + return; + SCHEMATIC* sch = Schematic(); if( sch != nullptr ) diff --git a/eeschema/schematic.cpp b/eeschema/schematic.cpp index 2bd03108bb..6766ce102a 100644 --- a/eeschema/schematic.cpp +++ b/eeschema/schematic.cpp @@ -37,6 +37,8 @@ #include #include +bool SCHEMATIC::m_IsSchematicExists = false; + SCHEMATIC::SCHEMATIC( PROJECT* aPrj ) : EDA_ITEM( nullptr, SCHEMATIC_T ), m_project( nullptr ), @@ -44,6 +46,7 @@ SCHEMATIC::SCHEMATIC( PROJECT* aPrj ) : { m_currentSheet = new SCH_SHEET_PATH(); m_connectionGraph = new CONNECTION_GRAPH( this ); + m_IsSchematicExists = true; SetProject( aPrj ); @@ -124,6 +127,8 @@ SCHEMATIC::~SCHEMATIC() delete m_currentSheet; delete m_connectionGraph; + + m_IsSchematicExists = false; } diff --git a/eeschema/schematic.h b/eeschema/schematic.h index 1ebc3cfe2f..df6221a584 100644 --- a/eeschema/schematic.h +++ b/eeschema/schematic.h @@ -310,6 +310,11 @@ public: */ void RemoveAllListeners(); + /** + * True if a SCHEMATIC exists, false if not + */ + static bool m_IsSchematicExists; + #if defined(DEBUG) void Show( int nestLevel, std::ostream& os ) const override {} #endif