Memory leak - dynamic loader - canvas



  • Hi.

    I use a loader for a dynamic load QML file. In the QML file, I use Canvas but when I'm changing a source file of the Loader, memory leak occurs. How do I can to prevent memory leaks?

    I use Qt v.5.8 with mingw53_32 on Windows 7, on Linux is the same problem.

    Here is a simple example, which occurs the memory leak.
    http://www.filedropper.com/untitled2_1

    Or here the code:
    main.cpp

    TestTimer test;
        QTimer *timer = new QTimer(&test);
        QObject::connect(timer,SIGNAL(timeout()),&test,SLOT(changeFile()));
        timer->start(20);
        _myComponent->loadUrl(QUrl(QStringLiteral("qrc:/main.qml")));
        _engine->rootContext()->setContextProperty("test",&test);
        QObject* _myComponentObj = _myComponent->create();
        return app.exec();
    

    testtimer.cpp

    QUrl TestTimer::urlFile(){
        return _urlFile;
    }
    
    void TestTimer::changeFile(){
        if(page1)
            _urlFile=QUrl(QLatin1String("qrc:/Page2.qml"));
        else
            _urlFile=QUrl(QLatin1String("qrc:/Page1.qml"));
        page1=!page1;
        emit urlFileChanged();
    }
    

    main.qml

        Loader{
            source: test.urlFile
        }
    

    Page1.qml and Page2.qml

    Canvas{
        property int borderSize:1
        height:100;width:100
        onPaint: {
            var centerLine = borderSize/2;
            var ctx = getContext("2d");
            ctx.lineCap="square";
            //ctx.translate(0.5, 0.5);
            ctx.strokeStyle = "red";
            ctx.lineWidth=borderSize;
            ctx.beginPath();
            ctx.lineTo(centerLine,height-borderSize+centerLine);
            ctx.lineTo(centerLine,borderSize/2);
            ctx.lineTo(width-borderSize+centerLine,borderSize/2);
            ctx.stroke();
            ctx.beginPath();
            ctx.moveTo(centerLine,height-borderSize+centerLine);
            ctx.lineTo(width-borderSize+centerLine,height-borderSize+centerLine);
            ctx.lineTo(width-borderSize+centerLine,centerLine);
            ctx.stroke();
        }
    }
    

    Thank you for help


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.