Here is my very simple code:
#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <iostream>
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
for(int i=0;i<2;i++){
ostringstream tmp;
tmp << "/vol/test1/" << i << ".jpg";
IplImage * img = cvLoadImage(tmp.str().c_str()); //line #12
IplImage* imgc = cvCreateImage(cvGetSize(img), img->depth,3);
cvCvtColor(img, imgc, CV_BGR2Lab); //line #14
cvReleaseImage(&img);
img = imgc;
cvReleaseImage(&img);
}
return 0;
}
The program just loads 2 images, and transforms them to CIELab space. However, Valgrind
throws the following errors:
==31879== LEAK SUMMARY:
==31879== definitely lost: 0 bytes in 0 blocks
==31879== indirectly lost: 0 bytes in 0 blocks
==31879== possibly lost: 0 bytes in 0 blocks
==31879== still reachable: 14,456 bytes in 6 blocks
==31879== suppressed: 0 bytes in 0 blocks
==31879==
==31879== ERROR SUMMARY: 903892 errors from 3 contexts (suppressed: 2 from 2)
Further check with -g --show-reachable=yes
gives me the details of the leak (the reports for block 1-4 are as the same as block 5 so I do not post it here):
==31879== 2,072 bytes in 1 blocks are still reachable in loss record 5 of 6
==31879== at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==31879== by 0x10B3DF5A: ??? (in /usr/lib/x86_64-linux-gnu/libpixman-1.so.0.30.2)
==31879== by 0x10B40D3F: ??? (in /usr/lib/x86_64-linux-gnu/libpixman-1.so.0.30.2)
==31879== by 0x10B03A68: ??? (in /usr/lib/x86_64-linux-gnu/libpixman-1.so.0.30.2)
==31879== by 0x400F305: call_init.part.0 (dl-init.c:85)
==31879== by 0x400F3DE: _dl_init (dl-init.c:52)
==31879== by 0x40016E9: ??? (in /lib/x86_64-linux-gnu/ld-2.15.so)
==31879==
==31879== 4,096 bytes in 1 blocks are still reachable in loss record 6 of 6
==31879== at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==31879== by 0x64870B7: libjpeg_general_init (in /usr/lib/x86_64-linux-gnu/libjpeg.so.8.0.2)
==31879== by 0x400F305: call_init.part.0 (dl-init.c:85)
==31879== by 0x400F3DE: _dl_init (dl-init.c:52)
==31879== by 0x40016E9: ??? (in /lib/x86_64-linux-gnu/ld-2.15.so)
Since the memory leak is "still reachable", I think I can safely ignore it. But it is the Error Summary of 903892 errors that concerns me. I rerun valgrind
with --track-origins=yes
:
==31879== ERROR SUMMARY: 903892 errors from 3 contexts (suppressed: 2 from 2)
==31879==
==31879== 301229 errors in context 1 of 3:
==31879== Use of uninitialised value of size 8
==31879== at 0x55219F0: cv::CvtColorLoop_Invoker<cv::RGB2Lab_b>::operator()(cv::Range const&) const (in /vol/Toolkits/opencv-2.4.10/release/lib/libopencv_imgproc.so.2.4.10)
==31879== by 0x5536152: cv::cvtColor(cv::_InputArray const&, cv::_OutputArray const&, int, int) (in /vol/Toolkits/opencv-2.4.10/release/lib/libopencv_imgproc.so.2.4.10)
==31879== by 0x55403A8: cvCvtColor (in /vol/Toolkits/opencv-2.4.10/release/lib/libopencv_imgproc.so.2.4.10)
==31879== by 0x400DEB: main (main.cpp:14)
==31879== Uninitialised value was created by a heap allocation
==31879== at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==31879== by 0x64ABA74: ??? (in /usr/lib/x86_64-linux-gnu/libjpeg.so.8.0.2)
==31879== by 0x64ABD02: ??? (in /usr/lib/x86_64-linux-gnu/libjpeg.so.8.0.2)
==31879== by 0x649EFF9: jinit_d_main_controller (in /usr/lib/x86_64-linux-gnu/libjpeg.so.8.0.2)
==31879== by 0x64A22BB: jinit_master_decompress (in /usr/lib/x86_64-linux-gnu/libjpeg.so.8.0.2)
==31879== by 0x64991D4: jpeg_start_decompress (in /usr/lib/x86_64-linux-gnu/libjpeg.so.8.0.2)
==31879== by 0x4E64621: cv::JpegDecoder::readData(cv::Mat&) (in /vol/Toolkits/opencv-2.4.10/release/lib/libopencv_highgui.so.2.4.10)
==31879== by 0x4E4AC8C: cv::imread_(std::string const&, int, int, cv::Mat*) (in /vol/Toolkits/opencv-2.4.10/release/lib/libopencv_highgui.so.2.4.10)
==31879== by 0x4E4B13B: cvLoadImage (in /vol/Toolkits/opencv-2.4.10/release/lib/libopencv_highgui.so.2.4.10)
==31879== by 0x400D9D: main (main.cpp:12)
(The reports for context 2 and 3 are as the same as context 1 so I do not post it here). The errors seem come from line 12 and 14 in my code. What is wrong? or Am I missing something here?
Side note: if I scan more images, the report for leak memory is the same, but number of errors in Error Summary
increases linearly. My program runs to segmentation fault
after scanning ~3000 images.
please, avoid all deprecated IplImages, and use cv::Mat, and the c++ api instead.
#include <opencv2/opencv.hpp> // c++ headers
#include <iostream>
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
for(int i=0;i<2;i++){
ostringstream tmp;
tmp << "/vol/test1/" << i << ".jpg";
Mat img = imread(tmp.str().c_str());
Mat imgc; // no pre-allocation nessecary
cvtColor(img, imgc, CV_BGR2Lab);
// no manual release nessecary
}
return 0;
}