QtQuick Leak on setSource or API misuse by me?

  • I'm running into a leak in Qt-Quick (4.7.4 and 4.8.0) when setting a new source using QDeclarativeView->setSource in response to a QML MouseArea click. I've been able to reproduce this with a very minimal application.

    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QtGui/QMainWindow>
    #include <QtDeclarative/QtDeclarative>

    class MainWindow : public QMainWindow
    QDeclarativeView * m_View;
    bool m_Toggle;
    MainWindow(QWidget *parent = 0);
    public slots:
    void changeView( );

    private slots:
    void setView( QString file );
    void toggleView();

    #endif // MAINWINDOW_H@

    @#include "mainwindow.h"
    #include <QTimer>

    MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent),m_Toggle(true)
    m_View = new QDeclarativeView(this);
    m_View->setSource( QUrl::fromLocalFile("dlg1.qml"));
    m_View->rootContext()->setContextProperty("mainWindow", this);

    // automated switching of dialog...
    // starting this timer does NOT leak!!!
    QTimer * t = new QTimer(this);
    connect(t, SIGNAL(timeout()), this, SLOT(toggleView()));
    // t->start();




    void MainWindow::changeView()
    // leaks!
    QMetaObject::invokeMethod(this, "toggleView", Qt::QueuedConnection);

    // leaks as well!
    //QTimer::singleShot(1, this, SLOT(toggleView()));


    void MainWindow::setView(QString file)
    m_View->setSource( QUrl::fromLocalFile(file));

    void MainWindow::toggleView()
    m_Toggle = !m_Toggle;
    if ( m_Toggle )

    dlg1.qml (and dlg2.qml with tiny change in text)
    @import QtQuick 1.0

    Rectangle {
    width: 480
    height: 272

    MouseArea {
        anchors.fill: parent;
        onClicked: {
    Text {
        anchors.centerIn: parent;
        text: "dialog 1\nclick me"


    So every time you click on the screen the app will switch to the other screen. Clicking repeatedly on the screen and looking at the memory use i can see the app memory use increasing. Now for the interesting bit. If I use the QTimer to do the switching I get no leak. But if I start a singleshot timer in response to the mouse click I do get a leak. Each click leaks about 5-10kB, which isn't much but in our full application it leaks about 1MB on each QML change, which on a mobile device will quickly run out of memory. I've tried deleting the view hoping it would release the memory, it did not.

    So before I post a bug on this I just want to make sure I'm not using the API incorrectly to display a different QML page.

  • I tried using a "QML loader":http://doc.qt.nokia.com/4.7-snapshot/qml-loader.html instead of calling setSource from C++ but that also leaks.

Log in to reply

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