Ask Your Question
0

opencv imshow causing a memory leak (c++)

asked 2012-12-03 02:07:59 -0600

kaga gravatar image

updated 2012-12-03 06:56:20 -0600

I wrote this method (it displays an image):

void ImageLoader::displayMyImage()
{
namedWindow("new_window1");
imshow("new_window1", m_image);
waitKey(2);
}

m_image is of Mat type.

I also use this destructor:

ImageLoader::~ImageLoader()
{
m_image.release();
}

However, Valgrind found tons of memory leaks. It's caused by these two cv functions: namedWindow and imshow (because without calling the displayMyImage() there is no any leak). Is there a way to fix it?

Edit: Here is the example code:

//Constructors (here I use only the first):
ImageLoader::ImageLoader(int width, int height)
    : m_image(width, height, CV_8UC3)
{

}

ImageLoader::ImageLoader(const string& fileName)
    : m_image(imread(fileName))
{
  if (!m_image.data)
  {
    cout << "Failed loading " << fileName << endl;
  }
}

//main ( where I call displayImage() ):
int main12(int argc, char **argv)
{
    ImageLoader img1("Lenna.png");
    img1.displayImage();
    return 0;
}

I'm sure it's the displayImage() method to cause memory leaks. Because it appears only when I call it.

Thanks!

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
2

answered 2012-12-03 02:29:06 -0600

Daniil Osokin gravatar image

Hi! Did you setup a real test (e.g. call this method)? If no, this leak is spurious. Check the comment for this question.

edit flag offensive delete link more

Comments

Yes, of course I've called displayMyImage() method. But what should I do in order to fix it?

kaga gravatar imagekaga ( 2012-12-03 02:46:04 -0600 )edit

Please, post the example code, where you call this. Can you try to invoke this method in a cycle and watch in system monitor the memory usage.

Daniil Osokin gravatar imageDaniil Osokin ( 2012-12-03 03:30:53 -0600 )edit

I'll do that. But can you tell me if the code is ok?

kaga gravatar imagekaga ( 2012-12-03 03:51:16 -0600 )edit
2

answered 2012-12-03 04:17:41 -0600

Vladislav Vinogradov gravatar image

namedWindow allocates some memory for inner structures. This memory will be freed when you close window or when you call destroyWindow / destroyAllWindows. Try to add destroyAllWindows to ImageLoaderer destructor.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2012-12-03 02:07:59 -0600

Seen: 4,383 times

Last updated: Dec 03 '12