[Interest] Memory leak in grabToImage?

Fabrice Mousset | GEOCEPT GmbH fabrice.mousset at geocept.com
Mon Aug 10 10:49:33 CEST 2020


Hi,

I am not a C++ guru, but I think your memory leak issue is linked with QSharedPointer usage.
I think you have to delete the connection to remove the lambda and destroy the QSharedPointer.

Like this:
void Utils::takeScreenshot()
{
    QQuickItem *item = m_rootView->contentItem();
    QSharedPointer<QQuickItemGrabResult> grab = item->grabToImage();
    if(grab.get() != nullptr) {
        connect(grab.get(), &QQuickItemGrabResult::ready, this, [grab]() {
            garb.get()->disconnect();
            qInfo() << "<<< Utils::takeScreenshot ";
        });
    }
}

Regards

Fabrice

Von: Interest <interest-bounces at qt-project.org> Im Auftrag von Marc Van Daele
Gesendet: Montag, 10. August 2020 10:32
An: Qt Interest <interest at qt-project.org>
Betreff: [Interest] Memory leak in grabToImage?

Hello,

The following example application has a memory leak when using 5.12.6 on Linux (when monitoring with htop, the memory of the process increases systematically).
Is this a programming error or a bug in Qt?

Looking forward to your input,

Marc

In QML, I take a screenshot every second

    Timer {

        id: memoryMonitor

        interval: 1*1000

        repeat: true

        onTriggered: utils.takeScreenshot()

    }

    Component.onCompleted: memoryMonitor.start()



In main.cpp, I register my Utils class as a context property

    Utils utils((QQuickWindow*)(app.allWindows()[0]));

    engine.rootContext()->setContextProperty("utils", &utils);



My Utils class looks like

class Utils : public QObject

{

    Q_OBJECT

public:

    explicit Utils(QQuickWindow* rootView, QObject *parent = nullptr);

    Q_INVOKABLE void takeScreenshot();

signals:

public slots:

private:

    QQuickWindow* m_rootView;

};

Utils::Utils(QQuickWindow* rootView, QObject *parent) : QObject(parent)

{

    m_rootView = rootView;

}



void Utils::takeScreenshot()

{

    QQuickItem *item = m_rootView->contentItem();

    QSharedPointer<QQuickItemGrabResult> grab = item->grabToImage();

    if(grab.get() != nullptr) {

        connect(grab.get(), &QQuickItemGrabResult::ready, this, [grab]() {

            qInfo() << "<<< Utils::takeScreenshot ";

        });

    }

}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20200810/2a7073be/attachment.html>


More information about the Interest mailing list