Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. qml memory leak
Qt 6.11 is out! See what's new in the release blog

qml memory leak

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
1 Posts 1 Posters 372 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    john shepard
    wrote on last edited by
    #1

    //TestMix.h
    #ifndef TESTMIX_H
    #define TESTMIX_H

    #include <QObject>
    #include <QSharedPointer>
    #include <QVariantMap>
    #include <QJSValue>
    #include <QJsonValue>
    #include <QVector>
    //#include "customdata.h"
    class TestMix : public QObject {
    Q_OBJECT
    public:
    explicit TestMix(QObject* parent=nullptr);
    TestMix(const TestMix& src);
    // Q_INVOKABLE void emitCustomData();
    // Q_INVOKABLE void emitCustomData2();
    Q_INVOKABLE void emitCustomData3();
    Q_INVOKABLE void emitCustomData4();
    Q_INVOKABLE void emitCustomData5();
    Q_INVOKABLE void emitCustomData6();
    Q_INVOKABLE void emitCustomData7();
    Q_INVOKABLE void emitCustomData8();
    Q_INVOKABLE void emitCustomData9();
    signals:
    //void testCustomData(CustomData* data);
    //void testCustomData2(QSharedPointer<CustomData> data);
    void testCustomData3(QVariantMap data);
    void testCustomData4(QByteArray data);

    void testCustomData5(QString data);
    void testCustomData6(QStringList data);
    void testCustomData7(QJSValue value);
    void testCustomData8(QString value);
    
    void testCustomData9(QList<int> data);
    

    };

    #endif // TESTMIX_H

    //testmix.cpp
    #include <QVariant>
    #include <QQmlEngine>
    #include <QJSEngine>
    #include <QVector>
    #include <QJsonObject>
    #include <QJsonArray>
    #include <QJsonDocument>
    #include <QDebug>
    #include "testmix.h"

    TestMix::TestMix(QObject* parent)
    : QObject(parent) {

    }

    //void TestMix::emitCustomData() {
    // CustomData* data = new CustomData(this);
    // data->setStrValue("Hello");
    // data->setIValue(12);
    // data->setFValue(1.2f);
    // data->setDValue(12.99);
    // QQmlEngine::setObjectOwnership(data, QQmlEngine::JavaScriptOwnership);
    // emit testCustomData(data);
    //}

    //void TestMix::emitCustomData2() {
    // QSharedPointer<CustomData> data(new CustomData(this));
    // data->setStrValue("Hello");
    // data->setIValue(12);
    // data->setFValue(1.2f);
    // data->setDValue(12.99);
    // emit testCustomData2(data);

    //}

    void TestMix::emitCustomData3() {
    QVariantMap d;
    d["strValue"] = "hello";
    d["iValue"] = 12;
    d["fValue"] = 12.04f;
    d["dValue"] = 12.333;
    d["trest"] = QByteArray(10000000,0);

    emit testCustomData3(d);
    

    }

    void TestMix::emitCustomData4() {
    QByteArray data(1000000, 0);
    emit testCustomData4(data);
    }

    void TestMix::emitCustomData5() {
    QString d;
    d.resize(1000000);
    emit testCustomData5(d);
    }

    void TestMix::emitCustomData6() {
    QStringList d;
    for (int i = 0; i < 100000; ++i) {
    d.append("xxxxx");
    }

    emit testCustomData6(d);
    d.clear();
    

    }

    void TestMix::emitCustomData7() {
    QJSEngine engine;
    QJSValue d = engine.newObject();
    d.setProperty("name", engine.newArray(1000000));
    d.setProperty("xx", 2);
    emit testCustomData7(d);
    }

    void TestMix::emitCustomData8() {
    qDebug() << "test 8";
    QJsonArray arr;
    for (int i = 0 ; i < 20000; ++i) {
    arr.append(i);
    }

    QJsonObject o;
    o["name"] = "test8";
    o["value"] = arr;
    
    QJsonDocument doc(o);
    QString json = doc.toJson();
    emit testCustomData8(json);
    

    }

    void TestMix::emitCustomData9() {
    QList<int> d;
    for (int i = 0; i < 100000; ++ i) {
    d.append(i);
    }
    emit testCustomData9(d);
    }

    //main.cpp
    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include "customdata.h"
    #include "testmix.h"
    int main(int argc, char *argv[])
    {
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);
    qmlRegisterType<TestMix>("TestMix", 1, 0, "TestMix");
    QQmlApplicationEngine engine;
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);
    
    return app.exec();
    

    }

    //main.qml
    import QtQuick 2.12
    import QtQuick.Window 2.12
    import QtQuick.Controls 2.12
    import TestMix 1.0

    Window {
    id: root
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    TestMix {
        id: testMix
    

    // onTestCustomData: {
    // for (var d in data) {
    // console.log(d, data[d])
    // }

    // console.log("@@@@@@@@@@########################")
    // console.log("strValue:", data.strValue
    // , " iValue:", data.iValue
    // , " fValue", data.fValue
    // , " dValue", data.dValue)

    // data.strValue = "1233"
    // console.log("strValue:", data.strValue)
    // data

    // }

    // onTestCustomData2: {

    // for (var d in data) {
    // console.log(d, data[d])
    // }

    // console.log("@@@@@@@@@@########################")
    // console.log("strValue:", data.strValue
    // , " iValue:", data.iValue
    // , " fValue", data.fValue
    // , " dValue", data.dValue)
    // }

        onTestCustomData3:  {
            for (var d in data) {
                console.log(d, data[d])
            }
    
            console.log("@@@@@@@@@@########################")
            console.log("strValue:", data.strValue
                        , " iValue:", data.iValue
                        , " fValue", data.fValue
                        , " dValue", data.dValue)
            //data.destroy()
        }
    
        onTestCustomData4: {
    
        }
    
        onTestCustomData5: {
    
        }
    
        onTestCustomData6: {
            //console.log("test 6")
            data = undefined
        }
    
        onTestCustomData7: {
            for (var d in value) {
                console.log(d, value[d])
            }
    
           console.log(typeof value)
           console.log(value["xx"])
           console.log(value.xx)
        }
    
        onTestCustomData8: {
            var d = JSON.parse(value)
           console.log(d["name"])
        }
    
        onTestCustomData9: {
            console.log(typeof data)
            data = undefined
        }
    
        signal emitJavaData(var d)
    
        onEmitJavaData: {
            d = undefined
        }
    }
    
    Flow {
        spacing: 10
        anchors.fill: parent
    

    // Button {
    // text: qsTr("测试")
    // onClicked: testMix.emitCustomData()
    // }

    // Button {
    // text: qsTr("测试2")
    // onClicked: testMix.emitCustomData2()
    // }

        Button {
            text: qsTr("测试3")
            onClicked: testMix.emitCustomData3()
        }
    
        Button {
            text: qsTr("测试4")
            onClicked: testMix.emitCustomData4()
        }
    
        Button {
            text: qsTr("测试5")
            onClicked: testMix.emitCustomData5()
        }
    
        Button {
            text: qsTr("测试6")
            onClicked: testMix.emitCustomData6()
        }
    
        Button {
            text: qsTr("测试7")
            onClicked: testMix.emitCustomData7()
        }
    
        Button {
            text: qsTr("测试8")
            onClicked: testMix.emitCustomData8()
        }
    
        Button {
            text: qsTr("测试9")
            onClicked: testMix.emitCustomData9()
        }
    
        Button {
            text: qsTr("测试10")
            onClicked: {
                var d = new Array(1000000);
                for (var i = 0; i < d.length;  ++i){
                    d[i] = Number(i);
                }
    
                testMix.emitJavaData(d)
                d = undefined
            }
        }
    }
    

    }

    question :
    clicked the button “测试6”
    i found the memory leak when i track the windows task manager memory

    how can I solve this problem?

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved