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. Memory leak - dynamic loader - canvas
QtWS25 Last Chance

Memory leak - dynamic loader - canvas

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
1 Posts 1 Posters 472 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.
  • P Offline
    P Offline
    Prochy
    wrote on last edited by Prochy
    #1

    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

    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