Browse Source

Better solution to memory leaks.

pull/18/head
Jeff Young 5 months ago
parent
commit
96dfff2f07
  1. 11
      eeschema/sch_design_block_utils.cpp
  2. 119
      eeschema/tools/sch_drawing_tools.cpp
  3. 15
      eeschema/tools/sch_editor_control.cpp
  4. 11
      pcbnew/pcb_design_block_utils.cpp
  5. 37
      pcbnew/tools/pcb_control.cpp

11
eeschema/sch_design_block_utils.cpp

@ -403,11 +403,11 @@ bool SCH_EDIT_FRAME::SaveSelectionToDesignBlock( const LIB_ID& aLibId )
}
}
DESIGN_BLOCK* blk = nullptr;
std::unique_ptr<DESIGN_BLOCK> blk;
try
{
blk = Prj().DesignBlockLibs()->DesignBlockLoad( aLibId.GetLibNickname(), aLibId.GetLibItemName() );
blk.reset( Prj().DesignBlockLibs()->DesignBlockLoad( aLibId.GetLibNickname(), aLibId.GetLibItemName() ) );
}
catch( const IO_ERROR& ioe )
{
@ -416,10 +416,7 @@ bool SCH_EDIT_FRAME::SaveSelectionToDesignBlock( const LIB_ID& aLibId )
}
if( !blk->GetSchematicFile().IsEmpty() && !checkOverwriteDbSchematic( this, aLibId ) )
{
delete blk;
return false;
}
// Create a temporary screen
SCH_SCREEN* tempScreen = new SCH_SCREEN( m_schematic );
@ -462,7 +459,6 @@ bool SCH_EDIT_FRAME::SaveSelectionToDesignBlock( const LIB_ID& aLibId )
{
DisplayErrorMessage( this, _( "Error saving temporary schematic file to create design block." ) );
wxRemoveFile( tempFile );
delete blk;
return false;
}
@ -472,7 +468,7 @@ bool SCH_EDIT_FRAME::SaveSelectionToDesignBlock( const LIB_ID& aLibId )
try
{
success = Prj().DesignBlockLibs()->DesignBlockSave( aLibId.GetLibNickname(), blk )
success = Prj().DesignBlockLibs()->DesignBlockSave( aLibId.GetLibNickname(), blk.get() )
== DESIGN_BLOCK_LIB_TABLE::SAVE_OK;
// If we had a group, we need to reselect it
@ -506,6 +502,5 @@ bool SCH_EDIT_FRAME::SaveSelectionToDesignBlock( const LIB_ID& aLibId )
m_designBlocksPane->RefreshLibs();
m_designBlocksPane->SelectLibId( blk->GetLibId() );
delete blk;
return success;
}

119
eeschema/tools/sch_drawing_tools.cpp

@ -646,17 +646,35 @@ int SCH_DRAWING_TOOLS::PlaceNextSymbolUnit( const TOOL_EVENT& aEvent )
int SCH_DRAWING_TOOLS::ImportSheet( const TOOL_EVENT& aEvent )
{
bool placingDesignBlock = aEvent.IsAction( &SCH_ACTIONS::placeDesignBlock );
COMMON_SETTINGS* common_settings = Pgm().GetCommonSettings();
EESCHEMA_SETTINGS* cfg = m_frame->eeconfig();
SCHEMATIC_SETTINGS& schSettings = m_frame->Schematic().Settings();
SCH_SCREEN* screen = m_frame->GetScreen();
SCH_SHEET_PATH& sheetPath = m_frame->GetCurrentSheet();
KIGFX::VIEW_CONTROLS* controls = getViewControls();
EE_GRID_HELPER grid( m_toolMgr );
VECTOR2I cursorPos;
DESIGN_BLOCK* designBlock = nullptr;
wxString sheetFileName = wxEmptyString;
if( !cfg || !common_settings )
return 0;
if( m_inDrawingTool )
return 0;
bool placingDesignBlock = aEvent.IsAction( &SCH_ACTIONS::placeDesignBlock );
std::unique_ptr<DESIGN_BLOCK> designBlock;
wxString sheetFileName = wxEmptyString;
if( placingDesignBlock )
{
if( m_frame->GetDesignBlockPane()->GetSelectedLibId().IsValid() )
SCH_DESIGN_BLOCK_PANE* designBlockPane = m_frame->GetDesignBlockPane();
if( designBlockPane->GetSelectedLibId().IsValid() )
{
designBlock = m_frame->GetDesignBlockPane()->GetDesignBlock(
m_frame->GetDesignBlockPane()->GetSelectedLibId(), true, true );
designBlock.reset( designBlockPane->GetDesignBlock( designBlockPane->GetSelectedLibId(),
true, true ) );
if( !designBlock )
return 0;
@ -672,27 +690,11 @@ int SCH_DRAWING_TOOLS::ImportSheet( const TOOL_EVENT& aEvent )
sheetFileName = *importSourceFile;
}
COMMON_SETTINGS* common_settings = Pgm().GetCommonSettings();
EESCHEMA_SETTINGS* cfg = m_frame->eeconfig();
SCHEMATIC_SETTINGS& schSettings = m_frame->Schematic().Settings();
SCH_SCREEN* screen = m_frame->GetScreen();
SCH_SHEET_PATH& sheetPath = m_frame->GetCurrentSheet();
KIGFX::VIEW_CONTROLS* controls = getViewControls();
EE_GRID_HELPER grid( m_toolMgr );
VECTOR2I cursorPos;
if( !cfg || !common_settings )
return 0;
if( m_inDrawingTool )
return 0;
auto setCursor =
[&]()
{
m_frame->GetCanvas()->SetCurrentCursor( designBlock ? KICURSOR::MOVING
: KICURSOR::COMPONENT );
: KICURSOR::COMPONENT );
};
auto placeSheetContents =
@ -829,41 +831,41 @@ int SCH_DRAWING_TOOLS::ImportSheet( const TOOL_EVENT& aEvent )
// if we weren't provided one
if( sheetFileName.IsEmpty() )
{
wxString path;
wxString file;
wxString path;
wxString file;
if (!placingDesignBlock) {
if (!placingDesignBlock) {
if( sheetFileName.IsEmpty() )
{
path = wxPathOnly( m_frame->Prj().GetProjectFullName() );
file = wxEmptyString;
}
else
{
path = wxPathOnly( sheetFileName );
file = wxFileName( sheetFileName ).GetFullName();
}
if( sheetFileName.IsEmpty() )
{
path = wxPathOnly( m_frame->Prj().GetProjectFullName() );
file = wxEmptyString;
}
else
{
path = wxPathOnly( sheetFileName );
file = wxFileName( sheetFileName ).GetFullName();
}
// Open file chooser dialog even if we have been provided a file so the user
// can select the options they want
wxFileDialog dlg( m_frame, _( "Choose Schematic" ), path, file,
FILEEXT::KiCadSchematicFileWildcard(),
wxFD_OPEN | wxFD_FILE_MUST_EXIST );
// Open file chooser dialog even if we have been provided a file so the user
// can select the options they want
wxFileDialog dlg( m_frame, _( "Choose Schematic" ), path, file,
FILEEXT::KiCadSchematicFileWildcard(),
wxFD_OPEN | wxFD_FILE_MUST_EXIST );
FILEDLG_IMPORT_SHEET_CONTENTS dlgHook( cfg );
dlg.SetCustomizeHook( dlgHook );
FILEDLG_IMPORT_SHEET_CONTENTS dlgHook( cfg );
dlg.SetCustomizeHook( dlgHook );
if( dlg.ShowModal() == wxID_CANCEL )
return 0;
if( dlg.ShowModal() == wxID_CANCEL )
return 0;
sheetFileName = dlg.GetPath();
sheetFileName = dlg.GetPath();
m_frame->GetDesignBlockPane()->UpdateCheckboxes();
}
m_frame->GetDesignBlockPane()->UpdateCheckboxes();
}
if( sheetFileName.IsEmpty() )
return 0;
if( sheetFileName.IsEmpty() )
return 0;
}
// If we're placing sheet contents, we don't even want to run our tool loop, just add the items
@ -875,9 +877,6 @@ int SCH_DRAWING_TOOLS::ImportSheet( const TOOL_EVENT& aEvent )
m_toolMgr->RunAction( ACTIONS::selectionClear );
m_view->ClearPreview();
delete designBlock;
designBlock = nullptr;
return 0;
}
@ -921,11 +920,12 @@ int SCH_DRAWING_TOOLS::ImportSheet( const TOOL_EVENT& aEvent )
{
if( placingDesignBlock )
{
m_toolMgr->PostAction( SCH_ACTIONS::drawSheetFromDesignBlock, designBlock );
// drawSheet must delete designBlock
m_toolMgr->PostAction( SCH_ACTIONS::drawSheetFromDesignBlock, designBlock.release() );
}
else
{
// drawSheet must delete
// drawSheet must delete sheetFileName
m_toolMgr->PostAction( SCH_ACTIONS::drawSheetFromFile,
new wxString( sheetFileName ) );
}
@ -2950,9 +2950,10 @@ int SCH_DRAWING_TOOLS::DrawSheet( const TOOL_EVENT& aEvent )
bool isDrawSheetCopy = aEvent.IsAction( &SCH_ACTIONS::drawSheetFromFile );
bool isDrawSheetFromDesignBlock = aEvent.IsAction( &SCH_ACTIONS::drawSheetFromDesignBlock );
DESIGN_BLOCK* designBlock = nullptr;
SCH_SHEET* sheet = nullptr;
wxString filename;
std::unique_ptr<DESIGN_BLOCK> designBlock;
SCH_SHEET* sheet = nullptr;
wxString filename;
if( isDrawSheetCopy )
{
@ -2965,7 +2966,7 @@ int SCH_DRAWING_TOOLS::DrawSheet( const TOOL_EVENT& aEvent )
}
else if( isDrawSheetFromDesignBlock )
{
designBlock = aEvent.Parameter<DESIGN_BLOCK*>();
designBlock.reset( aEvent.Parameter<DESIGN_BLOCK*>() );
wxCHECK( designBlock, 0 );
filename = designBlock->GetSchematicFile();
}

15
eeschema/tools/sch_editor_control.cpp

@ -2994,8 +2994,9 @@ int SCH_EDITOR_CONTROL::PlaceLinkedDesignBlock( const TOOL_EVENT& aEvent )
return 1;
// Get the associated design block
DESIGN_BLOCK* designBlock = editFrame->GetDesignBlockPane()->GetDesignBlock( group->GetDesignBlockLibId(),
true, true );
DESIGN_BLOCK_PANE* designBlockPane = editFrame->GetDesignBlockPane();
std::unique_ptr<DESIGN_BLOCK> designBlock( designBlockPane->GetDesignBlock( group->GetDesignBlockLibId(),
true, true ) );
if( !designBlock )
{
@ -3011,13 +3012,12 @@ int SCH_EDITOR_CONTROL::PlaceLinkedDesignBlock( const TOOL_EVENT& aEvent )
msg.Printf( _( "Design block %s does not have a schematic file." ),
group->GetDesignBlockLibId().GetUniStringLibId() );
m_frame->GetInfoBar()->ShowMessageFor( msg, 5000, wxICON_WARNING );
delete designBlock;
return 1;
}
editFrame->GetDesignBlockPane()->SelectLibId( group->GetDesignBlockLibId() );
return m_toolMgr->RunAction( SCH_ACTIONS::placeDesignBlock, designBlock );
return m_toolMgr->RunAction( SCH_ACTIONS::placeDesignBlock, designBlock.release() );
}
@ -3041,8 +3041,9 @@ int SCH_EDITOR_CONTROL::SaveToLinkedDesignBlock( const TOOL_EVENT& aEvent )
return 1;
// Get the associated design block
DESIGN_BLOCK* designBlock = editFrame->GetDesignBlockPane()->GetDesignBlock( group->GetDesignBlockLibId(),
true, true );
DESIGN_BLOCK_PANE* designBlockPane = editFrame->GetDesignBlockPane();
std::unique_ptr<DESIGN_BLOCK> designBlock( designBlockPane->GetDesignBlock( group->GetDesignBlockLibId(),
true, true ) );
if( !designBlock )
{
@ -3052,8 +3053,6 @@ int SCH_EDITOR_CONTROL::SaveToLinkedDesignBlock( const TOOL_EVENT& aEvent )
return 1;
}
delete designBlock;
editFrame->GetDesignBlockPane()->SelectLibId( group->GetDesignBlockLibId() );
return m_toolMgr->RunAction( SCH_ACTIONS::saveSelectionToDesignBlock ) ? 1 : 0;

11
pcbnew/pcb_design_block_utils.cpp

@ -188,11 +188,11 @@ bool PCB_EDIT_FRAME::SaveBoardToDesignBlock( const LIB_ID& aLibId )
return false;
}
DESIGN_BLOCK* blk = nullptr;
std::unique_ptr<DESIGN_BLOCK> blk;
try
{
blk = Prj().DesignBlockLibs()->DesignBlockLoad( aLibId.GetLibNickname(), aLibId.GetLibItemName() );
blk.reset( Prj().DesignBlockLibs()->DesignBlockLoad( aLibId.GetLibNickname(), aLibId.GetLibItemName() ) );
}
catch( const IO_ERROR& ioe )
{
@ -201,10 +201,7 @@ bool PCB_EDIT_FRAME::SaveBoardToDesignBlock( const LIB_ID& aLibId )
}
if( !blk->GetBoardFile().IsEmpty() && !checkOverwriteDbLayout( this, aLibId ) )
{
delete blk;
return false;
}
// Save a temporary copy of the schematic file, as the plugin is just going to move it
wxString tempFile = wxFileName::CreateTempFileName( "design_block" );
@ -213,7 +210,6 @@ bool PCB_EDIT_FRAME::SaveBoardToDesignBlock( const LIB_ID& aLibId )
{
DisplayErrorMessage( this, _( "Error saving temporary board file to create design block." ) );
wxRemoveFile( tempFile );
delete blk;
return false;
}
@ -223,7 +219,7 @@ bool PCB_EDIT_FRAME::SaveBoardToDesignBlock( const LIB_ID& aLibId )
try
{
success = Prj().DesignBlockLibs()->DesignBlockSave( aLibId.GetLibNickname(), blk )
success = Prj().DesignBlockLibs()->DesignBlockSave( aLibId.GetLibNickname(), blk.get() )
== DESIGN_BLOCK_LIB_TABLE::SAVE_OK;
}
catch( const IO_ERROR& ioe )
@ -237,7 +233,6 @@ bool PCB_EDIT_FRAME::SaveBoardToDesignBlock( const LIB_ID& aLibId )
m_designBlocksPane->RefreshLibs();
m_designBlocksPane->SelectLibId( blk->GetLibId() );
delete blk;
return success;
}

37
pcbnew/tools/pcb_control.cpp

@ -1318,22 +1318,14 @@ int PCB_CONTROL::AppendDesignBlock( const TOOL_EVENT& aEvent )
if( !editFrame )
return 1;
DESIGN_BLOCK* designBlock = nullptr;
if( !editFrame->GetDesignBlockPane()->GetSelectedLibId().IsValid() )
return 1;
DESIGN_BLOCK_PANE* designBlockPane = editFrame->GetDesignBlockPane();
std::unique_ptr<DESIGN_BLOCK> designBlock( designBlockPane->GetDesignBlock( designBlockPane->GetSelectedLibId(),
true, true ) );
designBlock = editFrame->GetDesignBlockPane()->GetDesignBlock( editFrame->GetDesignBlockPane()->GetSelectedLibId(),
true, true );
if( !designBlock )
return 1;
wxString designBlockBoardFile = designBlock->GetBoardFile();
delete designBlock;
if( designBlock->GetBoardFile().IsEmpty() )
if( !designBlock || designBlock->GetBoardFile().IsEmpty() )
return 1;
PCB_IO_MGR::PCB_FILE_T pluginType = PCB_IO_MGR::KICAD_SEXP;
@ -1351,7 +1343,7 @@ int PCB_CONTROL::AppendDesignBlock( const TOOL_EVENT& aEvent )
do
{
ret = AppendBoard( *pi, designBlockBoardFile );
ret = AppendBoard( *pi, designBlock->GetBoardFile() );
} while( repeatPlacement && ret == 0 );
return ret;
@ -1378,8 +1370,9 @@ int PCB_CONTROL::PlaceLinkedDesignBlock( const TOOL_EVENT& aEvent )
return 1;
// Get the associated design block
DESIGN_BLOCK* designBlock = editFrame->GetDesignBlockPane()->GetDesignBlock( group->GetDesignBlockLibId(),
true, true );
DESIGN_BLOCK_PANE* designBlockPane = editFrame->GetDesignBlockPane();
std::unique_ptr<DESIGN_BLOCK> designBlock( designBlockPane->GetDesignBlock( group->GetDesignBlockLibId(),
true, true ) );
if( !designBlock )
{
@ -1389,10 +1382,7 @@ int PCB_CONTROL::PlaceLinkedDesignBlock( const TOOL_EVENT& aEvent )
return 1;
}
wxString designBlockBoardFile = designBlock->GetBoardFile();
delete designBlock;
if( designBlockBoardFile.IsEmpty() )
if( designBlock->GetBoardFile().IsEmpty() )
{
wxString msg;
msg.Printf( _( "Design block %s does not have a board file." ),
@ -1408,7 +1398,7 @@ int PCB_CONTROL::PlaceLinkedDesignBlock( const TOOL_EVENT& aEvent )
if( !pi )
return 1;
int ret = AppendBoard( *pi, designBlockBoardFile );
int ret = AppendBoard( *pi, designBlock->GetBoardFile() );
return ret;
}
@ -1434,8 +1424,9 @@ int PCB_CONTROL::SaveToLinkedDesignBlock( const TOOL_EVENT& aEvent )
return 1;
// Get the associated design block
DESIGN_BLOCK* designBlock = editFrame->GetDesignBlockPane()->GetDesignBlock( group->GetDesignBlockLibId(),
true, true );
DESIGN_BLOCK_PANE* designBlockPane = editFrame->GetDesignBlockPane();
std::unique_ptr<DESIGN_BLOCK> designBlock( designBlockPane->GetDesignBlock( group->GetDesignBlockLibId(),
true, true ) );
if( !designBlock )
{
@ -1445,8 +1436,6 @@ int PCB_CONTROL::SaveToLinkedDesignBlock( const TOOL_EVENT& aEvent )
return 1;
}
delete designBlock;
editFrame->GetDesignBlockPane()->SelectLibId( group->GetDesignBlockLibId() );
return m_toolMgr->RunAction( PCB_ACTIONS::saveSelectionToDesignBlock ) ? 1 : 0;

Loading…
Cancel
Save