In an OpenCV application, how do I identify the source of memory leak and fix it?

后端 未结 1 1269
不知归路
不知归路 2021-01-03 02:24

I have a memory leak in my OpenCV application. Its a medium size application with a dozon of classes and a few thousands lines of code. Somehow, I managed to produce a large

相关标签:
1条回答
  • 2021-01-03 02:52

    Not an answer, but a suggestion: Move from OpenCV C interface to C++. If properly used, it will minimize your chances for a leak, now and in the future. Its smart pointers embedded in the objects automatically free memory.

    In the worst case, you'll have a performance penalty (too many allocs/deallocs), but those are easy to spot in a profiler.

    The C++ interface is using

    Mat intead of IplImage, 
    Point instead of CvPoint, 
    cv::function() instead of cvFunction. 
    

    And you do not have to declare pointers to images:

    Mat src = imread("myfile.jpg");
    Mat gray; // note that I do not allocate it. 
    // This is done automatically in the next functions
    cv::cvtColor(src, gray, CV_BGR2GRAY);
    imshow("Gray image", gray);
    waitKey();
    

    If you have some legacy code, or a third-party that uses the other interface, it's easy to convert back and forth:

    Mat src(width, height, CV_8UC3);
    IplImage* legacyImg;
    legacyImg = &(IplImage)src;
    

    Other datatypes (like CvPoint) are automatically converted. CvSeq is replaced by std::vector<T>

    0 讨论(0)
提交回复
热议问题