Search code examples
c++opencvmemory-leaksdefault-parameters

Would calling the default parameter with `new` cause memory leak?


I am trying to write a function that returns the center point of biggest detected object. No problem here. The issue is that I also want to return the foreground mask from parameters, if the user wants to use it.

In order to handle this situation, my solution is as following:

cv::Point detect(const cv::Mat &img, cv::Mat &mask = *new cv::Mat());

If mask parameter is specified, it can be used in main. Does RAII works in the other case, or would there be a memory leak?

Note: I know I can overload the function by 2 lines, but I want to know is it possible to do it properly by using default parameters, and not using pointers (input type is strict).

Another note: If cv::noArray() or a similar function can be used, that is totally okay.

Example usage:

char ch = 0;
while (ch != 27) // ESC is pressed
{
    cap >> img;
    if (img.empty())
        break;
    cv::Mat mask;
    cv::Point pt = detect(img, mask);
    // or pt = detect(img);
    cv::imshow("original", img);
    cv::imshow("foreground", mask);
    ch = cv::waitKey(1);
}

Solution

  • Yes, that introduces a memory leak. You might instead consider using a tuple return value:

    std::tuple<cv::Point, cv::Mat> detect(const cv::Mat &img);
    

    or making mask a pointer:

    cv::Point detect(const cv::Mat &img, cv::Mat *mask = nullptr);
    

    You could also try to keep the same signature with a cast. This is ugly but it might work:

    cv::Point detect(const cv::Mat &img, cv::Mat &mask = const_cast<cv::Mat&>(static_cast<cv::Mat const&>(cv::Mat())));