简体   繁体   中英

QML object property memory management

If I have some object, Item or QtObject as a property of a QML element (let's say Item as the container, but I am also interested in the situation, when the containing object is QtObject), how is the memory management done?

Think about these following situations:

1:    property var someObject: { "key" : "value" }

2:    property Item someItem: Item { ... }

3:    property QtObject someQtObject: QtObject { ... }

Will the containing element be the parent? Will the memory of the property object be released, when the parent gets destroyed? Is this actually not a good thing to do, and might lead to memory leaks, unless the properties are deleted or released in code? And so on. Any insights?

Also, would it be beneficial to do something like this in such situation:

4:    property Item someItem: Item {
           parent: containingElementId
           ...
      }

I have sometimes the need to do things such as here, and not simply configure the object to be under the children default property of an item. Also, when the containing object is QtObject, there is no children default property, but memory still needs to be managed.

property Item someItem: Item { ... }

The item won't be parented, but it will be collected nevertheless. Setting a parent explicitly will ensure that it is visible in the respective parent.

Keep in mind that Item::parent is the visual parent, not the actual parent. So even if you explicitly set a parent that survives longer than the property holder object, the property value object will be collected nevertheless.

This is because unlike the visual parent that remains null, the "logical" parent object will be set to the property holder object.

QML's memory management is automatic and for most parts works as expected. Two things to keep in mind:

  • the memory manager is rather reluctant to release memory until it becomes very necessary to do so
  • in some "less orthodox" use cases it is known to fail to collect certain objects, or what's even worse - it is known to destroy objects that are still in use , even though the documentation states that no object will be collected if it has a valid parent or active references to it, such cases usually result in crashes even in the relatively "safe" qtquick runtime environment, my personal solution to this rare problem is to set object ownership to CPP, thus disabling qml's memory management for that object completely, meaning that object lifetime has to be managed manually as in the olden days of C programming

So basically:

  1. it's most likely ok, but even if it isn't there is nothing you can do about it, as there is no explicit deletion of JS objects

  2. will be collected but will not be visible

  3. will be collected (keep in mind QtObject doesn't expose any parent)

  4. will be collected but will also be visible, a surviving parent will not save it from deletion

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM