diff --git a/package_bgs/IMBS/IMBS.cpp b/package_bgs/IMBS/IMBS.cpp
index d80c3b4f2b2ddff2a2ca18ea000e86aec94dfc3d..8214fa55bd699089c0006abfd54419ccbf8289b2 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 383e0ae27bdfc4b3a15dc703331b943f06d4c47f..7b9683b70011666d7a2a40d0d144c0f82ac6c8b6 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;