Ask Your Question

kaga's profile - activity

2018-09-11 11:39:35 -0500 received badge  Notable Question (source)
2017-03-09 12:27:31 -0500 received badge  Popular Question (source)
2012-12-03 03:51:16 -0500 commented answer opencv imshow causing a memory leak (c++)

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

2012-12-03 03:49:39 -0500 received badge  Editor (source)
2012-12-03 02:46:04 -0500 commented answer opencv imshow causing a memory leak (c++)

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

2012-12-03 02:44:12 -0500 received badge  Supporter (source)
2012-12-03 02:07:59 -0500 asked a question opencv imshow causing a memory leak (c++)

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!