QML object property memory management

Miika Pirttilä

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.

dtech
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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

QML object property memory management

From Dev

QML Access object property by property name string

From Dev

Property, Threads, Memory management and Rock & Roll

From Dev

QML QQmlPropertyList - contained object lifetimes and 'memory rules'

From Dev

QML scope: property binding in child object failing

From Dev

Assign a variable to a property and modify the real object in QML

From Dev

How to change a property of a object in QML upon a hover?

From Dev

QML scope: property binding in child object failing

From Dev

Prevent QML Property Bindings when QML object isn't visible?

From Dev

Object life cycle in java and memory management?

From Dev

javascript memory management - deleting a complex object

From Dev

Java Memory management and when to destroy object

From Dev

Memory management. Byte copy of CF Object

From Dev

Java Memory management and when to destroy object

From Dev

How to bind a property to a singleton object property from QML

From Dev

Can the memory management of a property change if it's redefined in a class extension?

From Dev

Setting object type property in QML from C++

From Dev

Bind a QAbstractListModel derived listmodel member of an object in QML as Q_PROPERTY

From Dev

QML: What is the difference between a Component, Element, Property, Attribute, and Object?

From Dev

QML: What is the difference between a Component, Element, Property, Attribute, and Object?

From Dev

QML object var property is null QVariant(QJSValue, ) from cpp

From Dev

IOS Memory and Memory Management

From Dev

object1 as a property of object2 which is in turn a property of object1 - will result in memory leak?

From Dev

Entity Framework using lots of memory for object insert with blob or byte property

From Dev

Binding property of child property in qml

From Dev

Qml property hooks

From Dev

QML - required custom property

From Dev

Send object from c++ to qml. What about free memory?

From Dev

I want to set a Qt QML Combobox to a PyQt5 object property

Related Related

  1. 1

    QML object property memory management

  2. 2

    QML Access object property by property name string

  3. 3

    Property, Threads, Memory management and Rock & Roll

  4. 4

    QML QQmlPropertyList - contained object lifetimes and 'memory rules'

  5. 5

    QML scope: property binding in child object failing

  6. 6

    Assign a variable to a property and modify the real object in QML

  7. 7

    How to change a property of a object in QML upon a hover?

  8. 8

    QML scope: property binding in child object failing

  9. 9

    Prevent QML Property Bindings when QML object isn't visible?

  10. 10

    Object life cycle in java and memory management?

  11. 11

    javascript memory management - deleting a complex object

  12. 12

    Java Memory management and when to destroy object

  13. 13

    Memory management. Byte copy of CF Object

  14. 14

    Java Memory management and when to destroy object

  15. 15

    How to bind a property to a singleton object property from QML

  16. 16

    Can the memory management of a property change if it's redefined in a class extension?

  17. 17

    Setting object type property in QML from C++

  18. 18

    Bind a QAbstractListModel derived listmodel member of an object in QML as Q_PROPERTY

  19. 19

    QML: What is the difference between a Component, Element, Property, Attribute, and Object?

  20. 20

    QML: What is the difference between a Component, Element, Property, Attribute, and Object?

  21. 21

    QML object var property is null QVariant(QJSValue, ) from cpp

  22. 22

    IOS Memory and Memory Management

  23. 23

    object1 as a property of object2 which is in turn a property of object1 - will result in memory leak?

  24. 24

    Entity Framework using lots of memory for object insert with blob or byte property

  25. 25

    Binding property of child property in qml

  26. 26

    Qml property hooks

  27. 27

    QML - required custom property

  28. 28

    Send object from c++ to qml. What about free memory?

  29. 29

    I want to set a Qt QML Combobox to a PyQt5 object property

HotTag

Archive