Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to delete the instance (or release the memory) created by Repeater



  • I haven't found a way to release the memory alloced by the Repeater.
    Anyone could tell me how to delete the instance (or release the memory) created by Repeater.
    Thanks in advance.


  • Moderators

    @obt-young

    just a guess, as you don't show any code

    function clearRepeater(){
         myRepeater.model = undefined
         gc()
    }
    
    Column {
        Repeater {
           id:myRepeater
            model: ["apples", "oranges", "pears"]
            Text { text: "Data: " + modelData }
        }
    }
    


  • Thanks for you reply.
    I try your method in a example code as follows:

    import QtQuick 2.5
    import QtQuick.Window 2.2
    import QtQuick.Controls 1.4
    
    
    Window {
        visible: true
        width: 300
        height: 300
        title: qsTr("Hello World")
    
        Grid{
            anchors.fill: parent
            spacing: 10
    
            Repeater{
                id:rp
                model:3
                delegate: Component{
                    Rectangle{
                        id:rc
                        color:"red"
                        width: 20
                        height: 20
                    }
                }
            }
        }
    
        Button{
            id:rebtn
            anchors.bottom: parent.bottom
            anchors.right: parent.right
            width: 20
            height: 20
            text:"-"
            onClicked: {
                rp.model = undefined
                gc()
            }
        }
    
        Button{
            anchors.bottom: parent.bottom
            anchors.right:rebtn.left
            width: 20
            height: 20
            text:"+"
            onClicked: {
                rp.model=3
            }
        }
    }
    
    

    It seems doesn't work , the memory increased every time i click the "+" button , but does not decrease when i click the "-" button


  • Moderators

    @obt-young
    2 points.

    • How to you monitor the memory increase ?
    • The GC is unreliable, even when manually called. The nature of JS...

  • Moderators

    @obt-young said in How to delete the instance (or release the memory) created by Repeater:

    delegate: Component{
    Rectangle{
    id:rc
    color:"red"
    width: 20
    height: 20
    }
    }

    i don't know if thats the cause of the behavior you are seeing, but normally you do not need to wrap your delegate item in a Component.
    Also you can try to log the delegate's destruction to make sure if it actually gets deleted or not.

    delegate: Rectangle{
            id:rc
            color:"red"
            width: 20
            height: 20
    
            Component.onDestruction: console.log("Repeater delegate destruction")
        }
    

    If you can see this message your delegate items are destroyed and your memory increase comes from somewhere else in your code.


Log in to reply