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
Developer
Posts: 1,746
Registered: ‎04-08-2010
My Device: Passport Silver, PRIV
My Carrier: Telekom.de, O2, Vodafone
Accepted Solution

deleteLater vs destroy dynamic objects in QML

from this Thread I know that its safe in C++ to use deleteLater(),

even if the user creates a new one before the old one was really destroyed

 

using dynamically created components in QML I'm always using .destroy() to delete them

 

in newer samples I noticed that instead of destroy() now deleteLater() was used in QML

 

What's recommended ?

and is deleteLater() in QML as safe as used from C++ ?

 

so this works fine ?

 

pseudocode

NavigationPane
property Page myPage

pushing the Page:
    myPage = ...created by Component...
    push myPage

coming back:
    onPopTransitionEnded()
        myPage.deleteLater

 

...if user just hits the Action to push the Page again before destroyed

 

works it the same as from C++ (explained by ZMey) ?

-------------------------------------------------------------------------------
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
Developer
Posts: 1,746
Registered: ‎04-08-2010
My Device: Passport Silver, PRIV
My Carrier: Telekom.de, O2, Vodafone

Re: deleteLater vs destroy dynamic objects in QML

ping

 

any hints ?

-------------------------------------------------------------------------------
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
Developer
Posts: 251
Registered: ‎11-22-2011
My Device: Blackberry Playbook
My Carrier: vodafone

Re: deleteLater vs destroy dynamic objects in QML

I can't answer the question but I have just recognized that regarding Sheets only destroy() works while deleteLater() throws an error "... is not a funtion". The codecompletion says the oppsit.

Developer
Posts: 1,746
Registered: ‎04-08-2010
My Device: Passport Silver, PRIV
My Carrier: Telekom.de, O2, Vodafone

Re: deleteLater vs destroy dynamic objects in QML

I'm still using .destroy() in QML,

but if you create a new qml file for NavigationPane with 10.2 IDE,

pages will be destroyed using .deleteLater()

that's why I'm asking what's recommended

 

-------------------------------------------------------------------------------
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
Developer
Posts: 828
Registered: ‎10-16-2012
My Device: Red Z10
My Carrier: Rogers

Re: deleteLater vs destroy dynamic objects in QML

I know in c++ when you call deleteLater() on an invocation it most certainly does not delete it....

Developer
Posts: 1,746
Registered: ‎04-08-2010
My Device: Passport Silver, PRIV
My Carrier: Telekom.de, O2, Vodafone

Re: deleteLater vs destroy dynamic objects in QML

deleteLater() on UI Controls works well from C++

see this Thread

-------------------------------------------------------------------------------
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
Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: deleteLater vs destroy dynamic objects in QML

[ Edited ]

Hi!

 

It's safe to use plain delete in most cases (I think destroy() in QML maps to C++ 'delete').

The problems appear in cases like this:

 

An API function is triggered, it emits a signal somewhere in the middle:

someFunction()
{
  ...skipped...
emit onPopTransitionDone(page); // here user's slot is called which deletes the page
...skipped...
does something with the page // Crash! }

To prevent this, Qt introduced deleteLater() which postpones object deletion until the next iteration of message loop.

 

We're being told in docs to use destroy() in onPopTransitionDone, so I think it's safe to assume that after emitting this signal, the page is not being used by NavigationPane. So it should be safe to use destroy(). 

deleteLater() will not hurt as well, it will postpone the deletion until the next iteration of message loop, but it's not strictly neccessary to use in this case.

 

Another example: in QNetworkAccessManager's onRequestFinished, the 'reply' is being used after exitting the slot, so deleteLater() is required there (and this is stated in docs).

 

For consistency it's better to use deleteLater() when deleting arguments in slots. delete (destroy()) is ok only if you know that the caller is not using the deleted object after emitting the signal. If it doesn't use it, this is usually explicitly stated in docs, otherwise deleteLater() is preferred.

 

 


Andrey Fidrya, @zmeyc on twitter
Developer
Posts: 1,746
Registered: ‎04-08-2010
My Device: Passport Silver, PRIV
My Carrier: Telekom.de, O2, Vodafone

Re: deleteLater vs destroy dynamic objects in QML

I'm always doing a

 

disconnect.mycppfunction(myqmlslot)

 

before destroy() a Page or so

(remember Simon Hain posted that destroy doesn't disconnect signals)

 

-----

so you would prefer a deleteLater() also in QML ?

 

the point I was unsure if this will work:

 

Page

   property MyPage myPage

 

where I'm creating myPage dynamically from ComponentDefinition and destroying coming back from onPopTransitionDone

 

if using deleteLater and user hits the Action again to create and push MyPage again,

I will assign the newly created MyPage to this property, before the first one was deleted (destroyed)

 

so you think this is ok, because under the hood the same Qt event bus is used as from C++ ?

-------------------------------------------------------------------------------
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: 57
Registered: ‎10-04-2012
My Device: developer
My Carrier: none

Re: deleteLater vs destroy dynamic objects in QML

Whatever happens to the myPage property after you call deleteLater will not change what happens to the object that deleteLater was called on. It will still be deleted. In fact, a safe bet is to set myPage to null after calling deleteLater so you cannot access it again. It doesn't matter if the 1st page was deleted after the 2nd page was created. The actual object myPage was pointing to before you change the value of the myPage property is not affected.

 

A note on signals:

DirectConnections have the behavior described above, but if that is not what you want you can use a QueuedConnection. See http://qt-project.org/doc/qt-4.8/qt.html#ConnectionType-enum for more details on this.

Developer
Posts: 1,746
Registered: ‎04-08-2010
My Device: Passport Silver, PRIV
My Carrier: Telekom.de, O2, Vodafone

Re: deleteLater vs destroy dynamic objects in QML

thanks verifying that deleteLater() is in QML as safe as in C++

 

so I will use deleteLater() instead of destroy()

-------------------------------------------------------------------------------
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