From 5ab4e955efb23564587bfdf9cb018b752e0e7579 Mon Sep 17 00:00:00 2001
From: Andrews Cordolino Sobral <andrewssobral@gmail.com>
Date: Sun, 31 Jul 2022 00:37:16 +0200
Subject: [PATCH] [important] Fixed memory leak on DP* algorithms

8 algorithms: DPAdaptiveMedian, DPEigenbackground, DPGrimsonGMM, DPMean, DPPratiMediod, DPTexture, DPWrenGA, DPZivkovicAGMM
---
 src/algorithms/DPAdaptiveMedian.cpp  |  1 +
 src/algorithms/DPEigenbackground.cpp |  1 +
 src/algorithms/DPGrimsonGMM.cpp      |  1 +
 src/algorithms/DPMean.cpp            |  1 +
 src/algorithms/DPPratiMediod.cpp     |  1 +
 src/algorithms/DPTexture.cpp         | 32 ++++++++++++++++------------
 src/algorithms/DPWrenGA.cpp          |  1 +
 src/algorithms/DPZivkovicAGMM.cpp    |  1 +
 8 files changed, 25 insertions(+), 14 deletions(-)

diff --git a/src/algorithms/DPAdaptiveMedian.cpp b/src/algorithms/DPAdaptiveMedian.cpp
index 5c34d3f..45d4044 100644
--- a/src/algorithms/DPAdaptiveMedian.cpp
+++ b/src/algorithms/DPAdaptiveMedian.cpp
@@ -61,6 +61,7 @@ void DPAdaptiveMedian::process(const cv::Mat &img_input, cv::Mat &img_output, cv
 
   img_foreground.copyTo(img_output);
   img_background.copyTo(img_bgmodel);
+  frame_data.ReleaseImage();
 
   firstTime = false;
   frameNumber++;
diff --git a/src/algorithms/DPEigenbackground.cpp b/src/algorithms/DPEigenbackground.cpp
index 9ddc1b9..9442be1 100644
--- a/src/algorithms/DPEigenbackground.cpp
+++ b/src/algorithms/DPEigenbackground.cpp
@@ -61,6 +61,7 @@ void DPEigenbackground::process(const cv::Mat &img_input, cv::Mat &img_output, c
 
   img_foreground.copyTo(img_output);
   img_background.copyTo(img_bgmodel);
+  frame_data.ReleaseImage();
 
   firstTime = false;
   frameNumber++;
diff --git a/src/algorithms/DPGrimsonGMM.cpp b/src/algorithms/DPGrimsonGMM.cpp
index d9d8e4c..ff3e16d 100644
--- a/src/algorithms/DPGrimsonGMM.cpp
+++ b/src/algorithms/DPGrimsonGMM.cpp
@@ -60,6 +60,7 @@ void DPGrimsonGMM::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Ma
 
   img_foreground.copyTo(img_output);
   img_background.copyTo(img_bgmodel);
+  frame_data.ReleaseImage();
 
   firstTime = false;
   frameNumber++;
diff --git a/src/algorithms/DPMean.cpp b/src/algorithms/DPMean.cpp
index cc106fa..caa17e4 100644
--- a/src/algorithms/DPMean.cpp
+++ b/src/algorithms/DPMean.cpp
@@ -60,6 +60,7 @@ void DPMean::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Mat &img
 
   img_foreground.copyTo(img_output);
   img_background.copyTo(img_bgmodel);
+  frame_data.ReleaseImage();
 
   firstTime = false;
   frameNumber++;
diff --git a/src/algorithms/DPPratiMediod.cpp b/src/algorithms/DPPratiMediod.cpp
index 17c9f98..c7bfada 100644
--- a/src/algorithms/DPPratiMediod.cpp
+++ b/src/algorithms/DPPratiMediod.cpp
@@ -61,6 +61,7 @@ void DPPratiMediod::process(const cv::Mat &img_input, cv::Mat &img_output, cv::M
 
   img_foreground.copyTo(img_output);
   img_background.copyTo(img_bgmodel);
+  frame_data.ReleaseImage();
 
   firstTime = false;
   frameNumber++;
diff --git a/src/algorithms/DPTexture.cpp b/src/algorithms/DPTexture.cpp
index ec5dd3e..920baf6 100644
--- a/src/algorithms/DPTexture.cpp
+++ b/src/algorithms/DPTexture.cpp
@@ -32,15 +32,15 @@ void DPTexture::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Mat &
   IplImage _frame = cvIplImage(img_input);
   frame = cvCloneImage(&_frame);
 
-  if (firstTime) {
-    width = img_input.size().width;
-    height = img_input.size().height;
-    size = width * height;
+  width = img_input.size().width;
+  height = img_input.size().height;
+  size = width * height;
 
-    // input image
-    image = cvCreateImage(cvSize(width, height), 8, 3);
-    cvCopy(frame, image.Ptr());
+  // input image
+  image = cvCreateImage(cvSize(width, height), 8, 3);
+  cvCopy(frame, image.Ptr());
 
+  if (firstTime) {
     // foreground masks
     fgMask = cvCreateImage(cvSize(width, height), 8, 1);
     tempMask = cvCreateImage(cvSize(width, height), 8, 1);
@@ -74,13 +74,13 @@ void DPTexture::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Mat &
     //erodeElement = cvCreateStructuringElementEx(3, 3, 1, 1,	CV_SHAPE_RECT);
     firstTime = false;
   }
-
-  cvCopy(frame, image.Ptr());
-
-  // perform background subtraction
-  bgs.LBP(image, texture);
-  bgs.Histogram(texture, curTextureHist);
-  bgs.BgsCompare(bgModel, curTextureHist, modeArray, dp::THRESHOLD, fgMask);
+  else {
+    // cvCopy(frame, image.Ptr());
+    // perform background subtraction
+    bgs.LBP(image, texture);
+    bgs.Histogram(texture, curTextureHist);
+    bgs.BgsCompare(bgModel, curTextureHist, modeArray, dp::THRESHOLD, fgMask);
+  }
 
   //if(enableFiltering)
   //{
@@ -113,6 +113,10 @@ void DPTexture::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Mat &
 
   // update background subtraction
   bgs.UpdateModel(fgMask, bgModel, curTextureHist, modeArray);
+
+  // free memory
+  image.ReleaseImage();
+  cvReleaseImage(&frame);
 }
 
 void DPTexture::save_config(cv::FileStorage &fs) {
diff --git a/src/algorithms/DPWrenGA.cpp b/src/algorithms/DPWrenGA.cpp
index 05b35a6..bb92f2f 100644
--- a/src/algorithms/DPWrenGA.cpp
+++ b/src/algorithms/DPWrenGA.cpp
@@ -59,6 +59,7 @@ void DPWrenGA::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Mat &i
 
   img_foreground.copyTo(img_output);
   img_background.copyTo(img_bgmodel);
+  frame_data.ReleaseImage();
 
   firstTime = false;
   frameNumber++;
diff --git a/src/algorithms/DPZivkovicAGMM.cpp b/src/algorithms/DPZivkovicAGMM.cpp
index a087b4f..9ba3565 100644
--- a/src/algorithms/DPZivkovicAGMM.cpp
+++ b/src/algorithms/DPZivkovicAGMM.cpp
@@ -59,6 +59,7 @@ void DPZivkovicAGMM::process(const cv::Mat &img_input, cv::Mat &img_output, cv::
 
   img_foreground.copyTo(img_output);
   img_background.copyTo(img_bgmodel);
+  frame_data.ReleaseImage();
 
   firstTime = false;
   frameNumber++;
-- 
GitLab