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

RadioButton is leaking when loaded and unloaded



  • Hi,
    I have encountered a problem with the QML Loader element and the QML RadioButton element. When I load and unload a QML Component that contains a RadioButton element I observe memory leaks.

    I have made a small example:

    main.cpp:

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        QGuiApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
        engine.load(QUrl(QLatin1String("qrc:/main.qml")));
    
        return app.exec();
    }
    

    main.qml:

    import QtQuick 2.1
    import QtQuick.Controls 1.0
    import QtQuick.Layouts 1.0
    
    ApplicationWindow {
        visible: true
        width: 640
        height: 480
        title: qsTr("Loader memory leak test")
    
        Loader {
            id: loader
            anchors {
                horizontalCenter: parent.horizontalCenter
                verticalCenter: parent.verticalCenter
            }
        }
    
        Row {
            anchors {
                bottom: parent.bottom
                horizontalCenter: parent.horizontalCenter
                margins: 10
            }
            spacing: 10
    
            Button {
                text: "Component 1"
    
                onClicked: {
                    loader.source = "Page1.qml"
                    gc()
                }
            }
    
            Button {
                text: "Component 2"
    
                onClicked: {
                    loader.source = "Page2.qml"
                    gc()
                }
            }
        }
    }
    
    

    Page1.qml:

    import QtQuick 2.1
    import QtQuick.Controls 1.2
    
    Item {
    
        Repeater {
            model: 1000
            anchors.fill: parent
    
            RadioButton {
                text: "Component 1"
            }
        }
    }
    

    Page2.qml:

    import QtQuick 2.1
    import QtQuick.Controls 1.2
    
    Item {
    
        Repeater {
            model: 1000
            anchors.fill: parent
    
            RadioButton {
                text: "Component 2"
            }
        }
    }
    

    Has anyone else experienced this issue? If that is the case, how did you solve it?


Log in to reply