Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PlotItem leak since PySide6 6.5.0 #2672

Closed
pijyoi opened this issue Apr 2, 2023 · 6 comments
Closed

PlotItem leak since PySide6 6.5.0 #2672

pijyoi opened this issue Apr 2, 2023 · 6 comments

Comments

@pijyoi
Copy link
Contributor

pijyoi commented Apr 2, 2023

Short description

Since PySide6 6.5.0, some objects within PlotItem will be leaked when PlotItem is destroyed.

This issue is a spinoff from #2665 as this issue is specific to PySide6 6.5.0.

Code to reproduce

Additional psutil library is used to show memory growth.

import pyqtgraph as pg
from pyqtgraph.Qt import QtCore
import psutil

app = pg.mkQApp()
timer = QtCore.QTimer()
proc = psutil.Process()

rss0 = proc.memory_info().rss

cnt = 0
def timer_cb():
    global cnt
    rss = proc.memory_info().rss
    print(f'{cnt}: {(rss - rss0)/1048576:.1f} MB')
    pg.PlotItem()
    cnt += 1
    if cnt > 100:
        timer.stop()
        app.exit()

timer.timeout.connect(timer_cb)
timer.start(0)
pg.exec()

Expected behavior

Memory usage should stabilize.

Real behavior

Memory usage continues to grow with each iteration.

Tested environment(s)

  • PyQtGraph version: master
  • Qt Python binding: PySide6 6.5.0
  • Python version: Python 3.10.10
  • NumPy version: 1.24.2
  • Operating system: Windows 11
  • Installation method: pip

Additional context

Doesn't occur for PySide2 5.12.2, PyQt5 5.15.9, PySide6 6.4.3, PyQt6 6.4.2

@pijyoi
Copy link
Contributor Author

pijyoi commented Apr 5, 2023

No leak for PyQt6 6.5.0 either.

@pijyoi
Copy link
Contributor Author

pijyoi commented Apr 5, 2023

Minimal pyqtgraph-free script that leaks on PySide >= 6.5.0

from PySide6 import QtCore, QtWidgets
import psutil

class Leaker:
    def __init__(self):
        parent = None
        parent = QtWidgets.QWidget()    # this line seemingly makes PySide < 6.5.0 not leak
        wgt= QtWidgets.QSpinBox(parent)
        wgt.valueChanged.connect(self.mkChangeCallback(wgt))

    def mkChangeCallback(self, w):
        return lambda *args: self.widgetChanged(w, *args)
        
    def widgetChanged(self, w, *args):
        pass

app = QtWidgets.QApplication([])
timer = QtCore.QTimer()
proc = psutil.Process()

rss0 = proc.memory_info().rss

cnt = 0
def timer_cb():
    global cnt
    rss = proc.memory_info().rss
    print(f'{cnt}: {(rss - rss0)/1048576:.1f} MB')
    Leaker()
    cnt += 1
    if cnt > 500:
        timer.stop()
        app.exit()

timer.timeout.connect(timer_cb)
timer.start(0)
app.exec() if hasattr(app, 'exec') else app.exec_()

@adrianghc
Copy link

adrianghc commented Apr 13, 2023

Hi, I'm one of the maintainers of Qt for Python. I'm currently investigating the leak and have reduced your script even further. The following code leaks on PySide >= 6.5.0, but not below:

from PySide6 import QtCore, QtWidgets


class Leaker:
    def __init__(self):
        widget = QtWidgets.QWidget()
        widget.windowIconChanged.connect(lambda *args: None)


app = QtWidgets.QApplication()
timer = QtCore.QTimer()
timer.timeout.connect(Leaker)
timer.start()
app.exec()

I'd like to encourage you to create a bug report in our JIRA detailing your findings. Feel free to assign it to me. Thank you very much for discovering this leak. 😊

@j9ac9k
Copy link
Member

j9ac9k commented Apr 15, 2023

Hi @adrianghc

The difference is the reference to self. We ran into this problem in another part of the library some eons ago and decided to just work around it at the time. We should have reported upsteam them, sorry!

@adrianghc
Copy link

We believe we have now fixed this bug: https://bugreports.qt.io/browse/PYSIDE-2299 It will be part of the PySide 6.5.1 release. Thank you for your patience, and let us know if you continue to have problems.

@pijyoi
Copy link
Contributor Author

pijyoi commented May 25, 2023

fixed in PySide 6.5.1 but also worked around in #2694

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants