C和OpenCV内存泄漏

C和OpenCV内存泄漏,c,opencv,memory-leaks,C,Opencv,Memory Leaks,我有内存泄漏的问题。在这个简单的程序中: #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("

我有内存泄漏的问题。在这个简单的程序中:

#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;
}

有人能告诉我如何正确地释放内存吗?谢谢你的建议。

@user4815162342可能是对的。
尽管我建议您迁移到新的
C++
OpenCv
cv::Mat
,通过它可以自动进行内存管理。

我已经多次看到这种漏洞。检查您发送的内容上面的详细错误。这可能是pixman库。

您没有证明内存泄漏,只是valgrind抱怨内存泄漏。复杂库有时使用分配模式,这些分配模式被valgrind等工具误传为泄漏。(示例包括分配池,或者出于性能原因在退出时不运行析构函数。)测试是否正在处理实际内存泄漏的一种简单方法是将
main()
的全部内容(除了
return
语句)包装在无限循环中。启动进程,并使用
top
或等效工具观察它:如果它的内存无限增长,那么您手头上就有泄漏。
==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)