[PyQt] Memory Leak on findChild

Hayward, Anthony Anthony.Hayward at sagentia.com
Tue Sep 5 15:21:50 BST 2017


New PyQt5 user here.

I was hoping someone could let me know if I'm doing something wrong, or if I've found a bug in PyQt5?

In my full application, every time findChild() is called on the view, the application uses more memory. Here's a simple example that replicates the issue:


# memoryTest.qml:
import QtQuick 2.0

Item {
    id: dummy
    width: 100; height: 100;
}


# memoryTest.py
import sys
from PyQt5.QtQuick import QQuickView
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtCore import QUrl, QObject, QTimer, pyqtSlot

class Controller(QObject):
    def __init__(self, view):
        """ Initialise the controller """
        QObject.__init__(self)

        self._view = view

        # Create timer loop to update the screen for text
        self._updateLoopTimer = QTimer()
        self._updateLoopTimer.timeout.connect(self.myTestSlot)
        self._updateLoopTimer.start(1000)

    @pyqtSlot()
    def myTestSlot(self):
        for _ in range(1000):
            thing = self._view.findChild(QObject, "aChildThatDontExist")
        print(thing)

if __name__ == '__main__':
    # Create main app
    app = QGuiApplication(sys.argv)

    view = QQuickView()
    ctrl = Controller(view)
    view.setSource(QUrl("memoryTest.qml"))
    view.rootContext().setContextProperty("ctrl", ctrl)
    view.show()

    # Wait till app closed
    app.exec_()

    # Exit this Python application
    sys.exit()



It doesn't seem to matter whether I call findChild() for an object that does or doesn't exist. The example above uses a bit under 400kB of memory a second.

Any help would be appreciated.

Thanks in advance,
Anthony
________________________________
This e-mail message is confidential and for use by the addressee only. If you are not the intended recipient, you must not use, disclose, copy or forward this transmission. Please return the message to the sender by replying to it and then delete the message from your computer. Sagentia provides e-mail services for both itself and its group companies. Sagentia shall not be held liable to any person resulting from the use of any information contained in this e-mail and shall not be liable to any person who acts or omits to do anything in reliance upon it. Sagentia does not accept responsibility for changes made to this message after it was sent.

Company Information: Name: Sagentia Limited. Registered Address: Harston Mill, Harston Cambridge CB22 7GG. Registered as a Company in England: 2081960 VAT Number: 432214202


More information about the PyQt mailing list