这是我非常简单的代码:
#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;
}
该程序仅加载 2 张图像,并将它们转换为 CIELab 空间。但是,Valgrind
会引发以下错误:
==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)
进一步检查-g --show-reachable=yes
给了我泄漏的详细信息(块 1-4 的报告与块 5 相同,所以我不在这里发布):
==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)
由于内存泄漏“仍然可以到达”,我想我可以放心地忽略它。但我关心的是 903892 错误的错误摘要。我重新valgrind
运行--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)
(上下文 2 和 3 的报告与上下文 1 相同,因此我不在此处发布)。错误似乎来自我的代码中的第 12 行和第 14 行。怎么了?还是我在这里遗漏了什么?
旁注:如果我扫描更多图像,泄漏内存的报告是相同的,但错误Error Summary
数量线性增加。我的程序segmentation fault
在扫描约 3000 张图像后运行。