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.
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.0Rectangle {
width: 480
height: 272MouseArea { 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.
-
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.