Expert Novice
Expert Novice

Reputation: 1963

memory leak in opencv functions

I have been noticing that my programs' memory usage keeps increasing pointlessly at times. Specially when i am using cvWaitKey(0), my program tends to shoot memory usage in 10 seconds to a huge size.

Is there a fix for this, or this is an OpenCv bug?

I have a simple function called Show_Image, to we which have set a callback function to get the pixel values:

void Show_Image(IplImage *img)
{
    cvNamedWindow("IMAGE_WINDOW", CV_WINDOW_AUTOSIZE); 
    cvSetMouseCallback("IMAGE_WINDOW", GETPIXEL , (void*)img);
    cvShowImage("IMAGE_WINDOW", img );
    cvWaitKey(0);
    cvDestroyWindow("IMAGE_WINDOW");
}

Upvotes: 3

Views: 1132

Answers (1)

Sam
Sam

Reputation: 20056

It seems there is no error and no workaround for this. When you call cvWaitkey(), the function processes all the windows message queue. And because you have a mouse callback, it always processes something there. This, combined with the weak capability of the system to show the real memory usage for a process, can give you a false leak alarm.

However, to find the source of the problem, add in your program a line of code, like below

getchar();

Wait for the process memory to increase, then press any key to get out of the message loop in cvWaitKey(), and then wait for a minute in the getchar() function. If the memory did not go down in that minute, it may be a leak. Feel free to fill a ticket in the openCV bug tracker https://code.ros.org/trac/opencv/wiki

Also try different methods to measure the increase in memory: How to measure actual memory usage of an application or process? or Tracking CPU and Memory usage per process

Upvotes: 2

Related Questions