Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Highlighted
New Developer
Posts: 58
Registered: ‎04-04-2012
My Device: Z30, Z10LE, Q5 & PlayBook
My Carrier: German Provider

memory leak with Image (bb/cascades/Image)

Hi there!

 

For my app ReadItNow! I download some images from the web for offline usage. Depending on the amount of existing data and contained images, this could be few hundred (or even more) images. 

 

I store the binary data as a QByteArray. When I read the data and want to show it inside the ListView for example, I need to make an Image from the QByteArray data. Following the code that I use to do so:

 

if (resultList.size() > 0) {
	QVariantMap map;
	QByteArray ba;
	QImage image, swappedImage;
	ImageData imageData;
	Image img;
	bool countEmitted = false;

	for (int c=0; c < resultList.size(); c++) {
		map = resultList.at(c).toMap();

		qDebug() << "creating thumbnail " << QString::number(c) << ": " << map.value("resolved_title").toString();
		ba = map.value("thumbnail").toByteArray();

		if (ba.size() > 0) {
			if (image.loadFromData(ba)) {
				swappedImage = image.rgbSwapped();
				if (!swappedImage.isNull()) {
					swappedImage = swappedImage.convertToFormat(QImage::Format_ARGB32_Premultiplied);
					imageData = ImageData&colon;:fromPixels(swappedImage.bits(), bb::PixelFormat::RGBX, swappedImage.width(), swappedImage.height(), swappedImage.bytesPerLine());

					if (imageData.isValid()) {

						// MEMORY LEAK - START!!!
						img = Image(imageData);
						//qDebug() << "img: " << img;
						// MEMORY LEAK - END!!!

						map.insert("thumbnail", QVariant::fromValue(img));
						resultList.replace(c, map);
					}
				}
			}
		}
	}
}

Sadly this operation causes a memory leak. I've marked that line with comments above and below. After doing this operation for a few hundred times, the app crashes. When I monitor the memory usage I can see that the memory usage rapidly increases. When I comment

 

img = Image(imageData);

out, everything is fine and there is no memory leak.

 

What can I do to prevent the leak and the subsequent crashing of my app?

 

Best,

Sven

---
Developer ReadItNow!, PhotoUpload, WhereIs...?, Magic4Square and OrganizeMe!

http://sven-ziegler.com
Developer
Posts: 587
Registered: ‎04-01-2009
My Device: Z10, PlayBook
My Carrier: NA

Re: memory leak with Image (bb/cascades/Image)

I think I'm struggling with the same thing right now. Did you figure out how to free the memory?

Developer
Posts: 587
Registered: ‎04-01-2009
My Device: Z10, PlayBook
My Carrier: NA

Re: memory leak with Image (bb/cascades/Image)

What was actually leaking the memory was when I was doing:

 

cascadesImage = bbImageData;

imageView->setImage(cascadesImage);

 

(where cascadesImage is defined in the .h file as bb::cascades::Image)

 

It seems that if I then do:

 

cascadesImage = bb::cascades::Image();

imageView->setImage(cascadesImage);

 

... it releases the memory.

New Developer
Posts: 58
Registered: ‎04-04-2012
My Device: Z30, Z10LE, Q5 & PlayBook
My Carrier: German Provider

Re: memory leak with Image (bb/cascades/Image)

And what about bbImageData in your solution? How do you set it?

 

Or did you mean

 

cascadesImage = bb::cascades::Image(bbImageData);

 ?

 

Best,

Sven

---
Developer ReadItNow!, PhotoUpload, WhereIs...?, Magic4Square and OrganizeMe!

http://sven-ziegler.com