PDA

View Full Version : memory leak when copying Qimage data



klobauster_toomc
24th February 2010, 16:39
What is the best way (both in terms of time and of memory) to copy image data (to a Qlabel.pixmap or to another Qimage-object)?

1. i have an object (let's call manipulator), that does some image manipulation. To copy the content of the image back i use this method:


void manipulator::getPhaseMap(QImage *phasemap)
{
int w = _phasemap->width();
int h = _phasemap->height();
QRgb* pBits = (QRgb *) phasemap->bits();
QRgb* _pBits = (QRgb *) _phasemap->bits();

for (int y=0;y<h;y++)
{
for (int x=0;x<w;x++)
{
pBits++;
_pBits++;
}
}
}

I would have thought, that by copying the content of the pointers, no extra memory would be used. But actually each time this method is executed, it adds 4mb (the images are 1000x1000 pixels in size) to the application. Since i need to do that like once every second, this is a problem!

(i recently found out, i could simply do

*phasemap = *_phasemap;

but i guess this takes a little bit longer, since it's not only the image data that is being copied..)

2. Inside the manipulator-object i have the following method, that copies data back from another structure into the Qimage:


void manipulator::buf2im(int w, int h, fftw_complex *f, QImage *im, int imag, float border)
{
float col = 0;
double *bounds = new double[4];
find_maxmin(w,h,f,bounds);

QRgb* pBits = (QRgb*) im->bits();
for (int i=0;i<w;i++)
{
for (int j=0;j<h;j++)
{
if (imag == 1) col = 255 * (f[i*w+j][1] - border*bounds[2]) / (border*bounds[3] - border*bounds[2]);
else col = 255 * (f[i*w+j][0] - border*bounds[0]) / (border*bounds[1] - border*bounds[0]);
// *pBits = qRgb(col,col,col);
*pBits = col;
pBits++;
}
}
delete bounds;
}

When i use the line *pBits = col; i get 1mb of extra memory used. Using the *pBits = qRgb(col,col,col); i get 4mb! How am i supposed to change the image so that i don't get a memory leak?

3. In a similar problem, i then try to show the Qimage inside a Qlabel. This is what i do:


ui->phaseview->setPixmap(QPixmap::fromImage(_phaseimage.scaledToH eight(ui->phaseview->height())));

This also creates another 4mb of extra memory usage.

Could someone point me to the best way to avoid those leaks?

Thanks in advance. (i'm using Qt 4.6.2 on windows)

MSUdom5
24th February 2010, 18:25
Are you sure the original image format is a 32 bit format?

klobauster_toomc
25th February 2010, 08:33
Are you sure the original image format is a 32 bit format?

yes the first image is loaded from a jpg file and the second one is created with


QImage( _image.size(), QImage::Format_RGB32);