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

Posts: 17,012
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Lifecycle of c++ objects in QML

I am currently thinking about memory management.

For that i want to utilize the QObject parent tree that automatically cleans child objects when it itself is deleted.


I have several get methods that create new c++ objects (deserialized from a database).

My idea was to add a QObject* parent parameter to the get methods and set this parent for the newly created object.


In my main.qml i have this code to clean up after a page was popped:

 onPopTransitionEnded: {

Now my question:

Would it be feasible to use the page object as a parent for the QObjects retrieved in its context?

The idea is that the page gets deleted and all child objects with it.

feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Posts: 1,746
Registered: ‎04-08-2010
My Device: Passport Silver, PRIV
My Carrier: Telekom.de, O2, Vodafone

Re: Lifecycle of c++ objects in QML

same questions for me ;-)


BTW: I noticed that destroying a Page did NOT destroy the content

in my testcase I destroy a Page with a ListView inside and a GroudDataModel

after destroying the Page if testing from C++ if the objects of ListView and GroupDataModel are deleted, they're not: findChild finds them

I ran into this because I wanted to save memory dynamically creating and destroying Pages

In fact as workaround I have to do findChildren() and use last from returned list to get the current Objects



ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
BlackBerry Platinum Enterprise Partner
International Development native Mobile Business Apps
BlackBerry 10 | Qt Mobile (Android, iOS)
Workshops / Trainings / Bootcamps

blog: http://ekkes-corner.org
mobile-development: http://appbus.org Twitter: @ekkescorner
BlackBerry Development Advisor
Posts: 15,784
Registered: ‎07-09-2008
My Device: BlackBerry PRIV
My Carrier: Bell

Re: Lifecycle of c++ objects in QML

I don't think a Page that is destroyed is destroyed immediately.  It is shoved in a queue and deleted later.  If these objects are attached to another object, they won't be deleted until the parent object is in the process of being deleted.  Also, in C++, memory is not wiped so if an object is deleted and the memory is not reallocated, it retains its contents which  may include pointers that allow functions like findChild / findChildren to still work.


If you want to make sure everything is freed at some point and you are not going to do anything silly like access it again, you can use this method and you should be fine.  If you need to do something complicated to manage objects, don't rely on this mechanism too much and use it carefully. 

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker