在OpenCV中使用CascadeClassifier的detectMultiScale函数是否存在内存泄漏问题?

3
如标题所述。我正在尝试在图像上运行一个简单的人脸检测器:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/objdetect/objdetect.hpp>

using namespace cv;

int main(int argc, char** argv)
{
  Mat image = imread("V2.jpg", 1);
  CascadeClassifier face_cascade;
  face_cascade.load("haarcascade_frontalface_alt.xml");
  vector<Rect> faces;
  face_cascade.detectMultiScale(image, faces);
  return 0;
}

根据Valgrind的输出,以下代码从detectMultiScale函数中泄漏。这里有什么好的做法被忽略了吗?需要释放什么吗?从逻辑上讲,一切都在我的栈上,所以当程序结束时它应该会被释放。
Valgrind的输出如下:
==4852==    at 0x4C28F9F: malloc (vg_replace_malloc.c:236)
==4852==    by 0x4EB1D90: cv::fastMalloc(unsigned long) (in /usr/lib/libopencv_core.so.2.3.1)
==4852==    by 0x58F175D: ??? (in /usr/lib/libopencv_objdetect.so.2.3.1)
==4852==    by 0x58F8699: cvHaarDetectObjectsForROC(void const*, CvHaarClassifierCascade*, CvMemStorage*, std::vector<int, std::allocator<int> >&, std::vector<double, std::allocator<double> >&, double, int, int, CvSize, CvSize, bool) (in /usr/lib/libopencv_objdetect.so.2.3.1)
==4852==    by 0x58EA38B: cv::CascadeClassifier::detectMultiScale(cv::Mat const&, std::vector<cv::Rect_<int>, std::allocator<cv::Rect_<int> > >&, std::vector<int, std::allocator<int> >&, std::vector<double, std::allocator<double> >&, double, int, int, cv::Size_<int>, cv::Size_<int>, bool) (in /usr/lib/libopencv_objdetect.so.2.3.1)
==4852==    by 0x58DA6B5: cv::CascadeClassifier::detectMultiScale(cv::Mat const&, std::vector<cv::Rect_<int>, std::allocator<cv::Rect_<int> > >&, double, int, int, cv::Size_<int>, cv::Size_<int>) (in /usr/lib/libopencv_objdetect.so.2.3.1)

这是在Windows 7 64位系统上运行的64位Kubuntu 11.10 VMware虚拟机中执行的。所使用的OpenCV版本是最新的 - 2.3.1。

2个回答

0

关于Valgrind的错误消息,我不确定,但是如果确实存在内存泄漏问题,那肯定不是你的代码的问题。不过建议你尝试升级到2.4.2版本,它也更快。


0

你必须释放变量 faces


faces 超出作用域时,它不会被销毁吗?并且在销毁时,向量将调用其所持有的每个对象的析构函数。或者我错了吗? - Micka

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接