PDA

View Full Version : QImage.scanLine pointer memory leak



rno
7th July 2016, 18:58
Hi,

I have a piece of code to display images one after the other. I noticed a memory leak when I "play" 100 images or so.

Here is the code

QImage image = QImage(width, height, QImage::Format_RGB32);
uint * scanLine;
for (int i = 0; i < height; i++)
{
scanLine = (uint *) image.scanLine(i);
for (int j = 0; j < width; j++)
{
pixel = 0xff - c[i * width + j];
scanLine[j] =qRgb(pixel, pixel, pixel); //memory leak here
}
}
delete [] c;

The image is displayed correctly but I cannot figure out what I am doing wrong here and why there is a memory leak.
height and width are 3000. c[] is a table of char with the image values. The image is a greyscale.

Can anyone help me solving this memory leak?

Cheers,
Arnaud

anda_skoa
7th July 2016, 22:27
The image should delete all of the memory it has allocated.

Which leak checker did you use that says that the QImage leaks its memory?

Cheers,
_

rno
8th July 2016, 09:31
Hi,

I am developing on linux and I use the system monitor. it is pretty clear. Each time I run the above code in loop to display successively 100 images the is a leak of ~3.5GB. If I run the loop again an additional 3.5GB is leaked. When I shutdown the app all memory is freed.

Commenting out scanLine[j] =qRgb(pixel, pixel, pixel); solve the problem. I'll try to reproduce with simple test code and try to find my error.

Cheers,
Rno

anda_skoa
8th July 2016, 13:04
And the QImage itself goes out of scope after it has been used?

Cheers,
_

rno
8th July 2016, 16:28
Hi,

Yes the image is out of scope but you guided me toward the solution by simplifying the code.

The problem is not the code above but below (I should have included it)

m_scene->addPixmap(QPixmap::fromImage(GetImageFromDetector( ExpData.FileAndPath)))

I am adding a Pixmap to Qgraphicscene rather than replacing.

m_scene->clear();
m_scene->addPixmap(QPixmap::fromImage(GetImageFromDetector( ExpData.FileAndPath)));


solved the problem.
Thanks a lot for making me think.
Rno

d_stranz
8th July 2016, 17:58
Thanks a lot for making me think.

Thinking is one of those things we should all do from time to time. It would hurt so much less if we used our heads for thinking rather than banging them on the wall.