Opencv namedWindow leak (C ++ and opencv)

Running valgrind, I get a lot of memory leaks in opencv, especially with the namedWindow function.

Basically I have a CSImg and PGImg image:

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); 

Image Display Function:

 void ImageHandler::displayImage(std::string& windowname){ namedWindow(windowname); imshow(windowname, m_image); waitKey(7000); } 

Valgrind gives me huge memory leaks when I display Image. For instance:

 ==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 line 33:

 imshow(windowname, m_image); //the full function is written above ^ 

randomU.cpp line 601:

 CSImg.displayImage(cs); 

Any help is appreciated. Ask for additional information that you need.

+6
source share
2 answers

Sorry, harsh reality looks like an OpenCV leak. It also flows from its Qt interface due to self-references according to the Leakage Tool (Xcode tools).

Another proof that this is not just a false signal: on my Mac, Opencv 2.4.3 is constantly growing in memory (according to Activity Monitor) when processing webcam input. (I do not use any pointers or data, so theoretically my OpenCV program should remain constant.)

+1
source

In fact, you no longer need to call namedWindow . You just call it "naked" cv::imshow(windowname,m_image) . It works great even if you overwrite.

Comment:
waitKey has two use cases:
1. wait forever, then waitKey(0);
2. Wait a bit, perhaps because you are showing input from a webcam. Then do waitKey(30); (or less, depending on the fps of what you play. For movies, 30.)

0
source

All Articles