Advertisement
Guest User

Mem leak PySide2 - heavy window creation + disposal

a guest
Sep 29th, 2019
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.72 KB | None | 0 0
  1. import gc
  2. import logging
  3. import os
  4.  
  5. import psutil
  6. from PySide2 import QtWidgets, QtCore
  7.  
  8.  
  9. def create_timer():
  10.     timer = QtCore.QTimer(qApp)
  11.     timer.setSingleShot(True)
  12.     timer.setInterval(10)
  13.     timer.start()
  14.     return timer
  15.  
  16.  
  17. logging.basicConfig()
  18. L = logging.getLogger()
  19. L.setLevel(1)
  20. qApp = QtWidgets.QApplication(os.sys.argv)
  21. TIMER = create_timer()
  22. WINDOW = QtWidgets.QPushButton("Quit me!")
  23. WINDOW.clicked.connect(qApp.quit)
  24. WINDOW.show()
  25. COUNTER = 0
  26. ACTUAL_WINDOW = None
  27.  
  28.  
  29. class ManagedSubWindow(QtWidgets.QWidget):
  30.     """Class fixing the multiple concurrent window problem.
  31.    It "remembers" on the class level all its children, until they are to be
  32.    destroyed."""
  33.     active_sub_windows = set()  # register of still active windows - prevention
  34.  
  35.     # against garbage collecting and SigSeg (11) signal = exitcode 139
  36.  
  37.     def __init__(self, *args, **kwargs):
  38.         """Register the window on its creation"""
  39.         kwargs["parent"] = WINDOW
  40.         super().__init__(*args, **kwargs)
  41.         self.setWindowFlags(QtCore.Qt.Window |
  42.                             QtCore.Qt.WindowMinimizeButtonHint |
  43.                             QtCore.Qt.WindowMaximizeButtonHint |
  44.                             QtCore.Qt.WindowCloseButtonHint)
  45.         self.active_sub_windows.add(self)
  46.         self.internals = kwargs.get("internals")
  47.  
  48.     def closeEvent(self, ce):
  49.         """Un-register the window + delete what is inside"""
  50.         # L.debug("ManagedSubWindow.closeEvent received %s", ce)
  51.         if self not in self.active_sub_windows:
  52.             L.debug("This window was yet closed... strange.")
  53.         self.active_sub_windows.discard(self)
  54.         del self.internals
  55.         self.setParent(None)
  56.         ce.accept()
  57.         self.deleteLater()
  58.         ret_value = super().closeEvent(ce)
  59.         self.destroy(destroyWindow=True, destroySubWindows=True)
  60.         return ret_value
  61.  
  62.  
  63. def create_window():
  64.     """Create a subwindow"""
  65.     global COUNTER
  66.     global ACTUAL_WINDOW
  67.     global TIMER
  68.     COUNTER += 1
  69.     if not (COUNTER % 100):
  70.         gc.collect()
  71.         process = psutil.Process(os.getpid())
  72.         used_ram = process.memory_info().rss
  73.         used_ram /= 2. ** 20
  74.         L.debug("active_sub_windows count %s ; used_ram %s",
  75.                 len(ManagedSubWindow.active_sub_windows), used_ram)
  76.     ACTUAL_WINDOW = ManagedSubWindow()
  77.     ACTUAL_WINDOW.show()
  78.     TIMER = create_timer()
  79.     TIMER.timeout.connect(close_window)
  80.  
  81.  
  82. def close_window():
  83.     """Close a subwindow"""
  84.     global ACTUAL_WINDOW
  85.     global TIMER
  86.     ACTUAL_WINDOW.close()
  87.     TIMER = create_timer()
  88.     TIMER.timeout.connect(create_window)
  89.  
  90.  
  91. TIMER.timeout.connect(create_window)
  92. os.sys.exit(qApp.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement