← Back to team overview

kicad-developers team mailing list archive

[Patch] Fix some memory leaks

 

I was noticing there were some memory leaks inside the board/module classes
that got somewhat extreme in some cases (I saw ~300MB leaked from opening
and closing cvpcb in Eeschema when run without a project manager). This
patch adds some deletion to the destructors of the board/module classes, so
they now will delete their sub items.

I believe these classes are the respective owners of those pointers to the
sub items, and my testing doesn't show any problems with this, but if
anyone can see a case where deleting these sub items on destruction might
be an issue, let me know.

-Ian
From 93959c0c51f7a19ceb45c63d274888feb0107e2a Mon Sep 17 00:00:00 2001
From: Ian McInerney <Ian.S.McInerney@xxxxxxxx>
Date: Sun, 11 Aug 2019 22:30:33 +0200
Subject: [PATCH] Fix some memory leaks

Some elements of modules and boards were not deleted, so memory
was being leaked on some library loads and single-instance
pcbnew usage.
---
 kicad/kicad_manager_frame.cpp |  7 +++++++
 pcbnew/class_board.cpp        | 19 +++++++++++++++++++
 pcbnew/class_module.cpp       | 11 +++++++++++
 3 files changed, 37 insertions(+)

diff --git a/kicad/kicad_manager_frame.cpp b/kicad/kicad_manager_frame.cpp
index 2ff4df65e..9ea194849 100644
--- a/kicad/kicad_manager_frame.cpp
+++ b/kicad/kicad_manager_frame.cpp
@@ -165,6 +165,13 @@ KICAD_MANAGER_FRAME::KICAD_MANAGER_FRAME( wxWindow* parent, const wxString& titl
 
 KICAD_MANAGER_FRAME::~KICAD_MANAGER_FRAME()
 {
+    // Ensure there are no active tools
+    if( m_toolManager )
+        m_toolManager->DeactivateTool();
+
+    delete m_actions;
+    delete m_toolManager;
+
     m_auimgr.UnInit();
 }
 
diff --git a/pcbnew/class_board.cpp b/pcbnew/class_board.cpp
index bc640f218..0dbe59020 100644
--- a/pcbnew/class_board.cpp
+++ b/pcbnew/class_board.cpp
@@ -146,9 +146,28 @@ BOARD::~BOARD()
         Delete( area_to_remove );
     }
 
+    // Clean up the owned elements
     DeleteMARKERs();
     DeleteZONEOutlines();
 
+    // Delete the modules
+    for( auto m : m_modules )
+        delete m;
+
+    m_modules.clear();
+
+    // Delete the tracks
+    for( auto t : m_tracks )
+        delete t;
+
+    m_tracks.clear();
+
+    // Delete the drawings
+    for (auto d : m_drawings )
+        delete d;
+
+    m_drawings.clear();
+
     delete m_CurrentZoneContour;
     m_CurrentZoneContour = NULL;
 }
diff --git a/pcbnew/class_module.cpp b/pcbnew/class_module.cpp
index 9fa8405eb..84326ef64 100644
--- a/pcbnew/class_module.cpp
+++ b/pcbnew/class_module.cpp
@@ -142,9 +142,20 @@ MODULE::MODULE( const MODULE& aModule ) :
 
 MODULE::~MODULE()
 {
+    // Clean up the owned elements
     delete m_Reference;
     delete m_Value;
     delete m_initial_comments;
+
+    for( auto p : m_pads )
+        delete p;
+
+    m_pads.clear();
+
+    for( auto d : m_drawings )
+        delete d;
+
+    m_drawings.clear();
 }
 
 
-- 
2.21.0


Follow ups