简体   繁体   中英

C and OpenCV memory leaks

I have problem with memory leaks. In this simple program:

#include <stdlib.h>
#include <stdio.h>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"

int main() {
    CvMat *src;
    src = cvLoadImageM("p1.jpg", CV_LOAD_IMAGE_GRAYSCALE);
    cvSaveImage("p2.bmp", src, 0);  
    cvReleaseMat(&src);
    return 0;
}

According to valgrind, there are 7 leaks:

==10478== HEAP SUMMARY:
==10478==     in use at exit: 10,464 bytes in 7 blocks
==10478==   total heap usage: 154 allocs, 147 frees, 2,908,450 bytes allocated
==10478== 
==10478== LEAK SUMMARY:
==10478==    definitely lost: 0 bytes in 0 blocks
==10478==    indirectly lost: 0 bytes in 0 blocks
==10478==      possibly lost: 0 bytes in 0 blocks
==10478==    still reachable: 10,464 bytes in 7 blocks
==10478==         suppressed: 0 bytes in 0 blocks
==10478== Rerun with --leak-check=full to see details of leaked memory
==10478== 
==10478== For counts of detected and suppressed errors, rerun with: -v
==10478== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)

Can anybody tell me how to properly free memory? Thank you for your advice.

@user4815162342 is probably right. Though I recommend you migrate to new C++ OpenCv cv::Mat through which memory management is automatic.

I've seen this leak many times. Check the detailed errors above what you sent. It's probably the pixman lib.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM