ubuntu16.04系统下,用apt装的opencv,别用。由内存泄漏,坑人!

sudo apt install libopencv-dev安装的opencv

调用OpenCV的C接口,最基本的函数CvShowImage()

利用valgrind检查内存泄漏情况

测试代码如下:

//main.c
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/highgui/highgui_c.h"

#include <stdio.h>

int main(){
    const char* filename = "cat.jpg";
    IplImage* im = cvLoadImage(filename, -1);
    cvShowImage(filename, im);
    cvWaitKey(0);
    cvReleaseImage(&im);

    return 0;
}

图片:

编译:

gcc main.c -O0 -g `pkg-config --cflags opencv` `pkg-config --libs opencv` -lm

检查内存泄漏:

valgrind --leak-check=full  ./a.out

输出:

==3130== 518,444 bytes in 1 blocks are possibly lost in loss record 6,011 of 6,011
==3130==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3130==    by 0x4F1A92D: cv::fastMalloc(unsigned long) (in /usr/lib/x86_64-linux-gnu/libopencv_core.so.2.4.9)
==3130==    by 0x4EB4F74: cvCreateData (in /usr/lib/x86_64-linux-gnu/libopencv_core.so.2.4.9)
==3130==    by 0x4EB5070: cvCreateMat (in /usr/lib/x86_64-linux-gnu/libopencv_core.so.2.4.9)
==3130==    by 0x528C9F7: cvImageWidgetSetImage(_CvImageWidget*, void const*) (in /usr/lib/x86_64-linux-gnu/libopencv_highgui.so.2.4.9)
==3130==    by 0x528E51B: cvShowImage (in /usr/lib/x86_64-linux-gnu/libopencv_highgui.so.2.4.9)
==3130==    by 0x402102: main (main.c:9)
==3130==
==3130== LEAK SUMMARY:
==3130==    definitely lost: 0 bytes in 0 blocks
==3130==    indirectly lost: 0 bytes in 0 blocks
==3130==      possibly lost: 522,276 bytes in 31 blocks
==3130==    still reachable: 965,240 bytes in 10,496 blocks
==3130==                       of which reachable via heuristic:
==3130==                         length64           : 6,240 bytes in 96 blocks
==3130==                         newarray           : 1,968 bytes in 43 blocks
==3130==         suppressed: 0 bytes in 0 blocks
==3130== Reachable blocks (those to which a pointer was found) are not shown.
==3130== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==3130==
==3130== For counts of detected and suppressed errors, rerun with: -v
==3130== ERROR SUMMARY: 31 errors from 31 contexts (suppressed: 0 from 0)

查看apt装的opencv版本信息:

⚡ apt-cache show libopencv-dev
Package: libopencv-dev
Architecture: amd64
Version: 2.4.9.1+dfsg-1.5ubuntu1.1
Priority: optional
Section: universe/libdevel
Source: opencv
Origin: Ubuntu
Maintainer: Kubuntu Developers <[email protected]>
Original-Maintainer: Debian Science Team <[email protected]>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 658
Depends: libc6 (>= 2.14), libgcc1 (>= 1:3.0), libopencv-calib3d2.4v5, libopencv-core2.4v5, libopencv-highgui2.4v5, libopencv-imgproc2.4v5, libopencv-ml2.4v5, libopencv-objdetect2.4v5, libstdc++6 (>= 5.2), libopencv-core-dev (= 2.4.9.1+dfsg-1.5ubuntu1.1), libopencv-ml-dev (= 2.4.9.1+dfsg-1.5ubuntu1.1), libopencv-imgproc-dev (= 2.4.9.1+dfsg-1.5ubuntu1.1), libopencv-video-dev (= 2.4.9.1+dfsg-1.5ubuntu1.1), libopencv-objdetect-dev (= 2.4.9.1+dfsg-1.5ubuntu1.1), libopencv-highgui-dev (= 2.4.9.1+dfsg-1.5ubuntu1.1), libopencv-calib3d-dev (= 2.4.9.1+dfsg-1.5ubuntu1.1), libopencv-flann-dev (= 2.4.9.1+dfsg-1.5ubuntu1.1), libopencv-features2d-dev (= 2.4.9.1+dfsg-1.5ubuntu1.1), libopencv-legacy-dev (= 2.4.9.1+dfsg-1.5ubuntu1.1), libopencv-contrib-dev (= 2.4.9.1+dfsg-1.5ubuntu1.1), libopencv-ts-dev (= 2.4.9.1+dfsg-1.5ubuntu1.1), libopencv-photo-dev (= 2.4.9.1+dfsg-1.5ubuntu1.1), libopencv-videostab-dev (= 2.4.9.1+dfsg-1.5ubuntu1.1), libopencv-stitching-dev (= 2.4.9.1+dfsg-1.5ubuntu1.1), libopencv-gpu-dev (= 2.4.9.1+dfsg-1.5ubuntu1.1), libopencv-superres-dev (= 2.4.9.1+dfsg-1.5ubuntu1.1), libopencv-ocl-dev (= 2.4.9.1+dfsg-1.5ubuntu1.1), libopencv2.4-java (= 2.4.9.1+dfsg-1.5ubuntu1.1), libopencv2.4-jni (= 2.4.9.1+dfsg-1.5ubuntu1.1), libcv-dev (= 2.4.9.1+dfsg-1.5ubuntu1.1), libhighgui-dev (= 2.4.9.1+dfsg-1.5ubuntu1.1), libcvaux-dev (= 2.4.9.1+dfsg-1.5ubuntu1.1), pkg-config
Recommends: opencv-data
Breaks: libopencv-core-dev (<= 2.3.1-8)
Filename: pool/universe/o/opencv/libopencv-dev_2.4.9.1+dfsg-1.5ubuntu1.1_amd64.deb
Size: 168514
MD5sum: b5fdea625c74f48de673eaad968989ca
SHA1: 5bc883be2cdf8f18121076c311e2ec3035aad77f
SHA256: 19fd5d0e2d9ea50f4990c193409c651896031266cd069789b7f9cec7fbd74a1f
Homepage: http://opencv.org/
Description-en: development files for opencv
 This is a metapackage providing development package necessary for
 development of OpenCV (Open Computer Vision).
 .
 The Open Computer Vision Library is a collection of algorithms and sample
 code for various computer vision problems. The library is compatible with
 IPL (Intel's Image Processing Library) and, if available, can use IPP
 (Intel's Integrated Performance Primitives) for better performance.
 .
 OpenCV provides low level portable data types and operators, and a set
 of high level functionalities for video acquisition, image processing and
 analysis, structural analysis, motion analysis and object tracking, object
 recognition, camera calibration and 3D reconstruction.
Description-md5: f9dc67381f1013c39fe59842c79cbddf
Supported: 5y

Package: libopencv-dev
Priority: optional
Section: universe/libdevel
Installed-Size: 657
Maintainer: Kubuntu Developers <[email protected]>
Original-Maintainer: Debian Science Team <[email protected]>
Architecture: amd64
Source: opencv
Version: 2.4.9.1+dfsg-1.5ubuntu1
Depends: libc6 (>= 2.14), libgcc1 (>= 1:3.0), libopencv-calib3d2.4v5, libopencv-core2.4v5, libopencv-highgui2.4v5, libopencv-imgproc2.4v5, libopencv-ml2.4v5, libopencv-objdetect2.4v5, libstdc++6 (>= 5.2), libopencv-core-dev (= 2.4.9.1+dfsg-1.5ubuntu1), libopencv-ml-dev (= 2.4.9.1+dfsg-1.5ubuntu1), libopencv-imgproc-dev (= 2.4.9.1+dfsg-1.5ubuntu1), libopencv-video-dev (= 2.4.9.1+dfsg-1.5ubuntu1), libopencv-objdetect-dev (= 2.4.9.1+dfsg-1.5ubuntu1), libopencv-highgui-dev (= 2.4.9.1+dfsg-1.5ubuntu1), libopencv-calib3d-dev (= 2.4.9.1+dfsg-1.5ubuntu1), libopencv-flann-dev (= 2.4.9.1+dfsg-1.5ubuntu1), libopencv-features2d-dev (= 2.4.9.1+dfsg-1.5ubuntu1), libopencv-legacy-dev (= 2.4.9.1+dfsg-1.5ubuntu1), libopencv-contrib-dev (= 2.4.9.1+dfsg-1.5ubuntu1), libopencv-ts-dev (= 2.4.9.1+dfsg-1.5ubuntu1), libopencv-photo-dev (= 2.4.9.1+dfsg-1.5ubuntu1), libopencv-videostab-dev (= 2.4.9.1+dfsg-1.5ubuntu1), libopencv-stitching-dev (= 2.4.9.1+dfsg-1.5ubuntu1), libopencv-gpu-dev (= 2.4.9.1+dfsg-1.5ubuntu1), libopencv-superres-dev (= 2.4.9.1+dfsg-1.5ubuntu1), libopencv-ocl-dev (= 2.4.9.1+dfsg-1.5ubuntu1), libopencv2.4-java (= 2.4.9.1+dfsg-1.5ubuntu1), libopencv2.4-jni (= 2.4.9.1+dfsg-1.5ubuntu1), libcv-dev (= 2.4.9.1+dfsg-1.5ubuntu1), libhighgui-dev (= 2.4.9.1+dfsg-1.5ubuntu1), libcvaux-dev (= 2.4.9.1+dfsg-1.5ubuntu1), pkg-config
Recommends: opencv-data
Breaks: libopencv-core-dev (<= 2.3.1-8)
Filename: pool/universe/o/opencv/libopencv-dev_2.4.9.1+dfsg-1.5ubuntu1_amd64.deb
Size: 168612
MD5sum: ab6f095040684dba7c59452293d76733
SHA1: 639f0eb5434c9e58cd6dc7d463cf3678ebe0ec6b
SHA256: 298ea7a4d354317df60b0cb9bfd0aa2c090c93743c3e178564f709f3226e0cb0
Description-en: development files for opencv
 This is a metapackage providing development package necessary for
 development of OpenCV (Open Computer Vision).
 .
 The Open Computer Vision Library is a collection of algorithms and sample
 code for various computer vision problems. The library is compatible with
 IPL (Intel's Image Processing Library) and, if available, can use IPP
 (Intel's Integrated Performance Primitives) for better performance.
 .
 OpenCV provides low level portable data types and operators, and a set
 of high level functionalities for video acquisition, image processing and
 analysis, structural analysis, motion analysis and object tracking, object
 recognition, camera calibration and 3D reconstruction.
Description-md5: f9dc67381f1013c39fe59842c79cbddf
Homepage: http://opencv.org/
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Origin: Ubuntu
Supported: 9m
02-10 20:59