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. QtQuick Leak on setSource or API misuse by me?

QtQuick Leak on setSource or API misuse by me?

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 1 Posters 1.7k Views 1 Watching
  • 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.
  • A Offline
    A Offline
    altera_2011
    wrote on last edited by
    #1

    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.

    mainwindow.h
    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

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

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

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

    #endif // MAINWINDOW_H@

    mainwindow.cpp
    @#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);

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

    }

    MainWindow::~MainWindow()
    {

    }

    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 )
    {
    setView("dlg1.qml");
    }
    else
    {
    setView("dlg2.qml");
    }
    }@

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

    Rectangle {
    width: 480
    height: 272

    MouseArea {
        anchors.fill: parent;
        onClicked: {
            mainWindow.changeView();
        }
    }
    
    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.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      altera_2011
      wrote on last edited by
      #2

      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.

      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