运行valgrind,我在opencv中遇到大量内存泄漏,尤其是使用namedWindow函数。
总的来说,我有一张图片CSImg和PGImg:
std::string cs = "Computer Science Students";
std::string pg = "Politics and Government Students";
CSImg.displayImage(cs);
cv::destroyWindow(cs);
PGImg.displayImage(pg);
cv::destroyWindow(pg);
显示图像功能是:
void ImageHandler::displayImage(std::string& windowname){
namedWindow(windowname);
imshow(windowname, m_image);
waitKey(7000);
}
当我执行displayImage时,Valgrind给了我巨大的内存泄漏。
例如:
==6561== 2,359,544 bytes in 1 blocks are possibly lost in loss record 3,421 of 3,421
==6561== at 0x4C2B3F8: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6561== by 0x4F6C94C: cv::fastMalloc(unsigned long) (in /usr/lib/libopencv_core.so.2.3.1)
==6561== by 0x4F53650: cvCreateData (in /usr/lib/libopencv_core.so.2.3.1)
==6561== by 0x4F540F0: cvCreateMat (in /usr/lib/libopencv_core.so.2.3.1)
==6561== by 0x56435AF: cvImageWidgetSetImage(_CvImageWidget*, void const*) (in /usr/lib/libopencv_highgui.so.2.3.1)
==6561== by 0x5644C14: cvShowImage (in /usr/lib/libopencv_highgui.so.2.3.1)
==6561== by 0x5642AF7: cv::imshow(std::string const&, cv::_InputArray const&) (in /usr/lib/libopencv_highgui.so.2.3.1)
==6561== by 0x40CED7: ImageHandler::displayImage(std::string&) (imagehandler.cpp:33)
==6561== by 0x408CF5: main (randomU.cpp:601)
imagehandler.cpp,第33行是:
imshow(windowname, m_image); //the full function is written above ^
randomU.cpp第601行是:
CSImg.displayImage(cs);
感谢任何帮助。 询问您需要的任何进一步信息。
答案 0 :(得分:1)
对不起,严酷的现实看起来像OpenCV漏洞。根据泄漏仪器(XCode工具),由于自我参考,它也从Qt接口侧泄漏。
其他证据表明这不仅仅是误报:在我的Mac上,Opencv 2.4.3在处理网络摄像头输入时会在内存中持续增长(根据活动监视器)。 (我没有使用任何指针或数据存储,所以理论上我的OpenCV程序应该保持不变的大小。)
答案 1 :(得分:0)
实际上您不再需要致电namedWindow
了。你只需要打电话给“裸体”cv::imshow(windowname,m_image)
。即使你覆盖它也能正常工作。
REMARK:
waitKey
有两种用法:
1.永远等待,然后waitKey(0);
2.等待一点,可能是因为您正在显示网络摄像头的输入。然后做waitKey(30);
(或更少,取决于你正在播放的fps。对于电影,30。)