JasperJeuken / QNodeEditor

Node editor for PyQt5
BSD 3-Clause "New" or "Revised" License
20 stars 4 forks source link

Memory Leak when repeatedly setting the state of the same dialog #22

Open fiveoverthree opened 3 weeks ago

fiveoverthree commented 3 weeks ago

When opening and closing the slightly modified example dialog (examples/dialog/main.py) multiple times, this allocates a few extra MB of RAM each time the dialog is opened, which are never freed.

The problem can be traced down to dialog.editor.scene.set_state(state).

from nodes import ConstantNode, OperationNode, OutputNode, SquareRootNode
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton

from QNodeEditor import NodeEditorDialog

def run_dialog(state: dict):
    dialog.editor.scene.set_state(state)
    succeeded = dialog.exec()
    state.update(dialog.editor.scene.get_state())

if __name__ == "__main__":
    # Create an application and a main window with a button
    state = {}
    app = QApplication([])
    window = QMainWindow()
    dialog = NodeEditorDialog()
    dialog.editor.available_nodes = {
        "Constant": ConstantNode,
        "Math": {"Operation": OperationNode, "Square root": SquareRootNode},
        "Output": OutputNode,
    }
    dialog.editor.output_node = OutputNode

    # Create a button that when pressed opens the dialog
    button = QPushButton("Open node editor")
    state = {}
    button.clicked.connect(lambda _: run_dialog(state))
    window.setCentralWidget(button)

    # Start the application
    window.show()
    app.exec()

Details: QNodeEditor 1.0.6, Python 3.12 (virtualenv) OS:

fiveoverthree commented 3 weeks ago

See attached video. Memory usage can be seen in lower right corner. Screencast from 2024-09-20 12-14-13.webm