PDA

View Full Version : QImage buffer



rafaelsegundo
23rd June 2008, 17:01
Hi, I'm using this code:



QImage img(str);
if(img.isNull()) {
printf("Failed to open image\n");
return NULL;
}


but it seems that QImage doesn't delete the buffer when destroyed (at the end of the method where this block of code is in). I don't use the QImage to store my images (I convert to another structure)... so, I can't free this buffer memory and the system becomes with a lot of buffer memory without usage (I use a huge amount of images). Any idea? I'd like to get rid of this buffer.

Thanks, Rafael

jpn
23rd June 2008, 19:45
How do you check that? Relevant thread: It seems that Qt does not release unused memory (http://www.qtcentre.org/forum/f-qt-programming-2/t-it-seems-that-qt-does-not-release-unused-memory-8569.html)

rafaelsegundo
23rd June 2008, 23:48
Let me explain what happens using top:

First, I run my program and this is the memory usage:

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
11180 rafael 15 0 45424 26m 8372 S 0.0 5.3 0:00.56 AVP

After execute this code:


FrameRGB * Loaders::qt_loader(QString str) {
QImage img(str);
if(img.isNull()) {
printf("Failed to open image\n");
return NULL;
}
int w = img.width();
int h = img.height();
FrameRGB *res = new FrameRGB(w, h);
for(int y = 0; y < h; y++) {
for(int x = 0; x < w; x++) {
int color = img.pixel(x,y);
int R = qRed(color);
int G = qGreen(color);
int B = qBlue(color);
res->simpleSetPixel(y, x, PixelRGB(R, G, B));
}
}
return res;
}

This function loads an image using QImage... and the result is:


PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
11180 rafael 18 0 153m 121m 9928 S 0.0 24.1 0:04.78 AVP

In theory, after done some operations, the memory would back to ~45m... but it doesn't


PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
11180 rafael 15 0 151m 119m 9928 S 0.0 23.7 0:05.23 AVP

But, if I change this code to:


FrameRGB * Loaders::qt_loader(QString str) {
int w = 320;
int h = 240;
FrameRGB *res = new FrameRGB(w, h);
for(int y = 0; y < h; y++) {
for(int x = 0; x < w; x++) {
res->simpleSetPixel(y, x, PixelRGB(0, 0, 0));
}
}
return res;
}

In other words, I discarded the QImage loading method and replaced by a constant flat color. In this case, the memory comes back to ~45m! So, I deduced that something is wrong with the QImage. :(

ChristianEhrlicher
24th June 2008, 06:48
QImage works fine - it's your method to measure the memory usage which is wrong. As long as valgrind (or a similar tool) doesn't report a memory leak inside QImage you don't have to worry about it.