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
Developer
alupimp
Posts: 22
Registered: ‎03-05-2011
My Device: Not Specified

Memory Problems with Custom Control Image View

I created a Custom Control from C ("RemoteImageView") basically what it does is present a default image when it deploys and waits for another to be downloaded and replaces the default. I use it in several places in the app but i particularly had problems in the galleries, because the app locks, it occurs to me that maybe this is not releasing memory properly.
I enclose classes of the RemoteImageView and the Gallery.

I'm pushing the gallery through a navigationPane from which the next  signal is connected:
void App :: popFinished (bb :: cascades :: Page * page)
{
     delete page;
}
I was hoping by delete the page it will delete all its children (including RemoteImageViews) but apparently it is not doing it, maybe that's not how you delete them or maybe I'm not setting the correct kinship.

I hope you can help me, maybe it's something basic, please remember to be patient as I walked entirely in Java and did not have these problems ...

 

/*
 * RemoteImageView.cpp
 *
 *  Created on: Oct 2, 2012
 *      Author: aluialarid
 */

#include "RemoteImageView.h"
#include <bb/cascades/Container>
#include <bb/cascades/ImageView>
#include <bb/cascades/ScalingMethod>
#include <bb/cascades/DockLayout>

using namespace bb::cascades;

RemoteImageView::RemoteImageView(Container *parent) :
        CustomControl(parent) {
	//Q_UNUSED(parent);
	mRootContainer = new Container();
	mRootContainer->setLayout(new DockLayout);
	imageView = ImageView::create().image(
			QUrl("asset:///images/defaultarticlelist.png")).horizontal(
			HorizontalAlignment::Center).vertical(VerticalAlignment::Center);
	imageView->setScalingMethod(bb::cascades::ScalingMethod::AspectFit);
	mRootContainer->add(imageView);
	setRoot(mRootContainer);
	connect(this, SIGNAL(urlChanged(QString)), this, SLOT(onurlChanged()));
	//  connect(mRootContainer->layout(), SIGNAL(CreationCompleted()), this, SLOT(onurlChanged()));

}

RemoteImageView::~RemoteImageView() {
	//delete mRootContainer;
}

void RemoteImageView::loadImage() {
	qDebug() << murl;
	QNetworkRequest request = QNetworkRequest();
	request.setUrl(QUrl(murl));
	QNetworkAccessManager* nam = new QNetworkAccessManager(this);
	bool result = connect(nam, SIGNAL(finished(QNetworkReply*)), this,
			SLOT(onImageLoaded(QNetworkReply*)));
	Q_ASSERT(result);
	Q_UNUSED(result);

	nam->get(request);
}
void RemoteImageView::onurlChanged() {
	loadImage();

}
void RemoteImageView::onImageLoaded(QNetworkReply* reply) {
	if (reply->error() != QNetworkReply::NoError) {
		emit imageUnavailable();
		return;
	}
	Image image = Image(reply->readAll());
	imageView->setImage(image);

}
void RemoteImageView::seturl(QString url) {
	if (murl.compare(url) != 0) {
		murl = url;
		emit urlChanged(murl);
	}
}
QString RemoteImageView::URL() {
	return murl;
}

 

/*
 * RemoteImageView.h
 *
 *  Created on: Oct 2, 2012
 *      Author: aluialarid
 */

#ifndef REMOTEIMAGEVIEW_H_
#define REMOTEIMAGEVIEW_H_

#include <bb/cascades/CustomControl>
#include <bb/cascades/Container>
#include <bb/cascades/ImageView>
#include <bb/cascades/Container>
#include <QTNetwork>

namespace bb {
	namespace cascades {
		class Container;
	}
}
using namespace bb::cascades;

class RemoteImageView: public CustomControl {
	Q_OBJECT
	Q_PROPERTY(QString url READ URL WRITE seturl NOTIFY urlChanged)

public:
	RemoteImageView(Container *parent=0);
	virtual ~RemoteImageView();
	Container* mRootContainer;
	ImageView* imageView;
    Q_INVOKABLE void loadImage();
    void seturl(QString url);
    QString URL();

public	slots:
	void onImageLoaded(QNetworkReply* reply);
	void onurlChanged();
	signals:
			void imageUnavailable();
			void urlChanged(QString url);

private:
	QString murl;

};

#endif /* REMOTEIMAGEVIEW_H_ */

 

/* *
 *  Created on: Oct 2, 2012
 *      Author: aluialarid
 */
import bb.cascades 1.0
import custom.lib 1.0

Page {
    id: galleryPage
    Container {
        background: Color.create("#000000")
        verticalAlignment: VerticalAlignment.Fill
        HeaderField {
            layoutProperties: StackLayoutProperties {
                spaceQuota: 1
            }
        }
        ListView {
            layoutProperties: StackLayoutProperties {
                spaceQuota: 7
            }
            objectName: "galleryView"
            function itemType(data, indexPath) {
                return "item";
            }
            layout: GridListLayout {
                cellAspectRatio: 1.4
            }
            listItemComponents: [
                ListItemComponent {
                    type: "item"
                    RemoteImageView {
                        url: ListItemData.imgURL
                    }
                }
            ]
            onTriggered: {
                var affectedItem = dataModel.data(indexPath);
                cs.pushImagePage(affectedItem.title, affectedItem.imgURL);
            }
        }
    }
}

This is how i push the gallery: 

 

void App::pushGallery(QString key) {
	QMap<QString, GroupDataModel*>::iterator it = m_imageMap.find(key);
	if (it != m_imageMap.end()) {
		GroupDataModel* gdm = it.value();

		QmlDocument *pageQml =
				QmlDocument::create("asset:///GalleryPage.qml").parent(
						navigationPane);
		pageQml->setContextProperty("cs", this);

		Page *appPage = pageQml->createRootObject<Page>();

		ListView* galleryView = appPage->findChild<ListView *>("galleryView");
		galleryView->setDataModel(gdm);
		navigationPane->push(appPage);
	}
}

 thanks!

New Contributor
thadude3
Posts: 8
Registered: ‎01-13-2011
My Device: Not Specified

Re: Memory Problems with Custom Control Image View

[ Edited ]

did you ever figure this out? I wrote a very similar class and tried a bunch of different things to get the memory to release properly or something.  I am using it in a list view and all the images load correctly but when I scroll they get filled in with prevous images in memory. duplicating and triplicating. its like custom controls are not released properly via the parent.