From 250d6564e1f8e30f8de20a7c74cbd359a9534c19 Mon Sep 17 00:00:00 2001
From: Lieven Govaerts <lgo@apache.org>
Date: Sat, 10 Feb 2018 23:29:46 +0100
Subject: [PATCH] Fix a memory leak when destroying the IndependentMultimodal
 object.  (#109)

* Fix a memory leak when deosrtying the IndependentMultimodal object. Ensure internally used objects
stored in arrays are destroyed correctly.

* package_bgs/IMBS/IMBS.hpp
  (Bins, BgModel): Add initialize function and destructor to cover the lifecycle of its member variables.

* package_bgs/IMBS/IMBS.cpp
  (BackgroundSubtractorIMBS::initialize): Use the new Bins and BgModel initialize functions.

* Whitespace-only fix.

* package_bgs/IMBS/IMBS.hpp
  (~BgModel, ~Bins): Cleanup whitespaces.
---
 package_bgs/IMBS/IMBS.cpp | 11 ++---------
 package_bgs/IMBS/IMBS.hpp | 29 ++++++++++++++++++++++++++---
 2 files changed, 28 insertions(+), 12 deletions(-)

diff --git a/package_bgs/IMBS/IMBS.cpp b/package_bgs/IMBS/IMBS.cpp
index d80c3b4..8214fa5 100644
--- a/package_bgs/IMBS/IMBS.cpp
+++ b/package_bgs/IMBS/IMBS.cpp
@@ -173,15 +173,8 @@ void BackgroundSubtractorIMBS::initialize(Size frameSize, int frameType)
 
   for (unsigned int p = 0; p < numPixels; ++p)
   {
-    bgBins[p].binValues = new Vec3b[numSamples];
-    bgBins[p].binHeights = new uchar[numSamples];
-    bgBins[p].isFg = new bool[numSamples];
-
-    bgModel[p].values = new Vec3b[maxBgBins];
-    bgModel[p].isValid = new bool[maxBgBins];
-    bgModel[p].isValid[0] = false;
-    bgModel[p].isFg = new bool[maxBgBins];
-    bgModel[p].counter = new uchar[maxBgBins];
+    bgBins[p].initialize(numSamples);
+    bgModel[p].initialize(maxBgBins);
   }
 }
 
diff --git a/package_bgs/IMBS/IMBS.hpp b/package_bgs/IMBS/IMBS.hpp
index 383e0ae..7b9683b 100644
--- a/package_bgs/IMBS/IMBS.hpp
+++ b/package_bgs/IMBS/IMBS.hpp
@@ -131,16 +131,39 @@ private:
   Mat initialMsgRGB;
 
   //struct for modeling the background values for a single pixel
-  typedef struct {
+  typedef struct Bins {
+    void initialize(unsigned int numSamples) {
+      binValues = new Vec3b[numSamples];
+      binHeights = new uchar[numSamples];
+      isFg = new bool[numSamples];
+    }
+    ~Bins() {
+      if (binValues)  { delete[] binValues; }
+      if (binHeights) { delete[] binHeights; }
+      if (isFg)       { delete[] isFg; }
+    }
     Vec3b* binValues;
     uchar* binHeights;
     bool* isFg;
   } Bins;
-
   Bins* bgBins;
+
 public:
   //struct for modeling the background values for the entire frame
-  typedef struct {
+  typedef struct BgModel {
+    void initialize(unsigned int maxBgBins) {
+      values = new Vec3b[maxBgBins];
+      isValid = new bool[maxBgBins];
+      isValid[0] = false;
+      isFg = new bool[maxBgBins];
+      counter = new uchar[maxBgBins];
+    }
+    ~BgModel() {
+      if (values)  { delete[] values; }
+      if (isValid) { delete[] isValid; }
+      if (isFg)    { delete[] isFg; }
+      if (counter) { delete[] counter; }
+    }
     Vec3b* values;
     bool* isValid;
     bool* isFg;
-- 
GitLab