Ask Your Question
1

Memory leak with morphological operators

asked 2012-09-04 16:43:32 -0600

GP gravatar image

updated 2012-09-04 16:51:29 -0600

Hi everyone,

It might be a stupid question but..why Visual Studio (2010) says there's a memory leak here? I'm using OpenCV 2.4.1 with TBB support.

int main()
{
    Mat mask(480, 640, CV_8U, Scalar(255));
    cv::erode(mask, mask, cv::Mat());

    // Turn on memory leaks detection.
    _CrtSetDbgFlag((_CRTDBG_LEAK_CHECK_DF) | _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG));

    return 0;
}

Regards,

Giuliano

edit retag flag offensive close merge delete

Comments

Do you still have this leak if comment erode call?

Daniil Osokin gravatar imageDaniil Osokin ( 2012-09-05 03:59:40 -0600 )edit

Commenting the erode, the leak disappears. Same stuff using "dilate".

GP gravatar imageGP ( 2012-09-06 02:27:53 -0600 )edit

Thank you for response. Could you try to run erode in cycle and see in task manager is memory leaks or not. Now I'm on linux, in system monitor no memory leaks; checked with some tools, they aren't reported about leaks. May be it's windows specific?

Daniil Osokin gravatar imageDaniil Osokin ( 2012-09-06 03:05:31 -0600 )edit

If I run erode in a loop, the allocated memory does not increase. There's still only one memory leak. Checking where that memory is allocated, I can see some references to TBB code (starting from morph.cpp:1183).

GP gravatar imageGP ( 2012-09-06 06:44:41 -0600 )edit
1

I suppose, that in this example with erode threading isn't used, because tbb implementation doesn't support inplace calculation (source is a destination at the same time). Please, try to add cv::Ptr<tbb::task_scheduler_init>; tbb_init = new tbb::task_scheduler_init(); before erode call. Is now memory leak?

Daniil Osokin gravatar imageDaniil Osokin ( 2012-09-06 15:21:01 -0600 )edit

It seems the line of code fixes the issue! What's the explanation? I've never used TBB before.

GP gravatar imageGP ( 2012-09-07 08:38:57 -0600 )edit

Many thanks for testing, I've posted the answer.

Daniil Osokin gravatar imageDaniil Osokin ( 2012-09-10 01:55:35 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
3

answered 2012-09-10 01:54:01 -0600

Daniil Osokin gravatar image

Here is no memory leak.
As we know, OpenCV uses auto-initialization for TBB. So task dispatcher often will be destroyed when the application completes. This is the reason, that VS reports about memory leak. When you use local variable cv::Ptr, it will be destroyed as usual local variable.

You shouldn't care about when it will be destroyed (but it will) and use construction with cv::Ptr in usual application, like this example. I'm suggest to use more accurate tools for memory analyze in case of TBB, like this.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2012-09-04 16:43:32 -0600

Seen: 1,997 times

Last updated: Sep 10 '12