Schematic editor: fix some issues that can crash the editor on closing.

- Add a test to be sure a SCHEMATIC exists before trying to use it
- Delete items in Repeat list and undo/redo list before deleting the schematic
- Delete also these items when loading a new schematic (the repeat list was
not cleaned previously)
( include qa compatibility from master )
Before these fixes I saw a use after delete issue due to dangling pointers
stored in Repeat list.
From master branch
This commit is contained in:
jean-pierre charras 2024-06-14 16:57:57 +02:00
parent 944ee4af7d
commit 096566e857
6 changed files with 32 additions and 0 deletions

View File

@ -146,6 +146,7 @@ bool SCH_EDIT_FRAME::OpenProjectFiles( const std::vector<wxString>& aFileSet, in
// unload current project file before loading new
{
ClearUndoRedoList();
ClearRepeatItemsList();
SetScreen( nullptr );
m_toolManager->GetTool<EE_INSPECTION_TOOL>()->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<EE_SELECTION_TOOL>()->ClearSelection();
ClearUndoRedoList();
ClearRepeatItemsList();
if( setProject )
{
@ -1462,6 +1464,7 @@ bool SCH_EDIT_FRAME::importFile( const wxString& aFileName, int aFileType,
}
ClearUndoRedoList();
ClearRepeatItemsList();
initScreenZoom();
SetSheetNumberAndCount();

View File

@ -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;

View File

@ -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;
/**

View File

@ -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 )

View File

@ -37,6 +37,8 @@
#include <sim/spice_value.h>
#include <netlist_exporter_spice.h>
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;
}

View File

@ -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