[Interest] Memory leak in grabToImage?

Marc Van Daele marc.van.daele90 at gmail.com
Mon Aug 10 11:12:32 CEST 2020


Thanks!
This indeed fixes the leak.

Marc

On Mon, 10 Aug 2020 at 10:49, Fabrice Mousset | GEOCEPT GmbH <
fabrice.mousset at geocept.com> wrote:

> 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/737463f9/attachment.html>


More information about the Interest mailing list