在线程中使用OpenCv 3.2.0时内存泄漏

时间:2017-03-21 20:35:59

标签: c++ multithreading opencv c++11

在Win7上,在msys2上使用mingw-64工具链(但在linux gcc上也是如此),使用OpenCv-3.2.0(但它与3.1.0相同)。

以下是我试图尽可能简化的代码,以显示我的问题:

#include <thread>
#include <iostream>
#include <iomanip>

#include "opencv_detector.h"
#include <windows.h>
#include <psapi.h>

/**
* Returns the current resident set size (physical memory use) measured in bytes
*/
inline size_t getCurrentRSS( )
{
  PROCESS_MEMORY_COUNTERS info;
  GetProcessMemoryInfo( GetCurrentProcess( ), &info, sizeof(info) );
  return (size_t)info.WorkingSetSize;
}

int main(int argc, char** argv)
{
  auto opencvDetector = new wl::ds::OpencvDetector();
  cv::Mat img = cv::imread("./test.png");
  int i = 0;
  long rss = getCurrentRSS();
  long rss_start = rss;
  long rss1 = rss;
  std::cout << std::setw(7) << "START " << rss <<std::endl;
  while(i++<1000) {
    // If I just call the function no memory leak
    //opencvDetector->process(img);
    // If I call the function in a thread, memory leak
    auto opencvDetectProfile = std::thread(&wl::ds::OpencvDetector::process, opencvDetector, img);
    opencvDetectProfile.join();

    rss1 = getCurrentRSS();
    if(rss!=rss1) {
      std::cout << std::setw(6) << i << " " << rss1 << " (" << std::setw(8) << (rss1-rss) << " / " << std::setw(7) << (rss1-rss_start) << ")" << std::endl;
      rss = rss1;
    }
  }
  std::cout << std::setw(7) << "STOP " << rss1 << " (" << std::setw(8) << (rss1-rss) << " / " << std::setw(7) << (rss1-rss_start) << ")" << std::endl;
  return 0;
}

这是非常简单的openCvDetector类:

#ifndef WL_DS_OPENCVDETECTOR_H
#define WL_DS_OPENCVDETECTOR_H

#include <string>
#include <opencv2/opencv.hpp>

namespace wl {
namespace ds {


class OpencvDetector {
 private:
   cv::CascadeClassifier cascadeClassifier;
 public:
   OpencvDetector() { cascadeClassifier.load("haarcascade_frontalface.xml"); };
   void process(cv::Mat img) {
     cv::cvtColor(img, img, CV_BGR2GRAY);
     cv::equalizeHist(img, img);
     std::vector<cv::Rect> faces;
     cascadeClassifier.detectMultiScale(img, faces, 1.05, 3, 0|CV_HAAR_SCALE_IMAGE);
   };
};

} // wl
} // ds

#endif // WL_DS_OPENCVDETECTOR_H

如果我直接调用opencvDetector->process(img);,内存使用会迅速稳定下来:

START 18280448
     1 30892032 (12611584 / 12611584)
     2 30908416 (   16384 / 12627968)
     4 30928896 (   20480 / 12648448)
     5 30941184 (   12288 / 12660736)
     8 30945280 (    4096 / 12664832)
    11 30953472 (    8192 / 12673024)
    12 30957568 (    4096 / 12677120)
  STOP 30957568 (       0 / 12677120)

但是如果我运行线程版本(在线程中调用OpenCvDetector的成员函数),内存使用量会不断增长:

START 18280448                      
    1 30965760 (12685312 / 12685312)
    2 30879744 (  -86016 / 12599296)
    3 30883840 (    4096 / 12603392)
    8 30916608 (   32768 / 12636160)
   10 31035392 (  118784 / 12754944)
   11 31047680 (   12288 / 12767232)
   12 30961664 (  -86016 / 12681216)
   14 30965760 (    4096 / 12685312)
   20 30969856 (    4096 / 12689408)
  120 31346688 (  376832 / 13066240)
  121 31084544 ( -262144 / 12804096)
  153 31088640 (    4096 / 12808192)
  213 31092736 (    4096 / 12812288)
  233 31096832 (    4096 / 12816384)
  248 31100928 (    4096 / 12820480)
  294 31105024 (    4096 / 12824576)
  335 31109120 (    4096 / 12828672)
  417 31113216 (    4096 / 12832768)
  499 31117312 (    4096 / 12836864)
  504 31121408 (    4096 / 12840960)
  540 31125504 (    4096 / 12845056)
  581 31129600 (    4096 / 12849152)
  665 31133696 (    4096 / 12853248)
  749 31137792 (    4096 / 12857344)
  781 31141888 (    4096 / 12861440)
  833 31145984 (    4096 / 12865536)
  848 31207424 (   61440 / 12926976)
 STOP 31207424 (       0 / 12926976)

我很失望,因为我想在很多线程中实现很多OpenCv检测,所以看起来有点内存泄漏正在迅速成为资源的巨大浪费。 任何提示都将不胜感激......

1 个答案:

答案 0 :(得分:1)

您在所有线程中使用相同的opencvDetector,这可能导致未定义的行为。在你的情况下,它会导致内存泄漏。

尝试这种方式:

auto opencvDetector = new wl::ds::OpencvDetector();
auto opencvDetectProfile = std::thread(&wl::ds::OpencvDetector::process, opencvDetector, img);
opencvDetectProfile.join();
delete opencvDetector;