Aladin
Aladin

Reputation: 512

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?

Thanks!

Upvotes: 1

Views: 2078

Answers (1)

Barney Szabolcs
Barney Szabolcs

Reputation: 12514

Your first problem is that you name the named window differently: "new_Window1" is different from "new_window1". Second, I tell you I have never used namedWindow, you only need to use imshow to display an image in an image window called "new_window1".

Remark1: you don't need to worry about explicitly releasing m_image, that is what Mat is for in the first place.

Remark2: waitKey(0) holds the window forever.

I have seen this question here before, so I think you could search here too for answers.

Upvotes: 2

Related Questions