From 364337083f18dece198baa79616c2610333c9c8a Mon Sep 17 00:00:00 2001 From: Luke Campagnola <> Date: Mon, 18 Jun 2012 14:01:51 -0400 Subject: [PATCH] Added workaround for Qt crash-at-exit bug (make sure that all GraphicsItems live in a scene before exiting) --- __init__.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/__init__.py b/__init__.py index e968d355..4c768a54 100644 --- a/__init__.py +++ b/__init__.py @@ -119,11 +119,27 @@ from .SignalProxy import * from .ptime import time +## Workaround for Qt exit crash: +## ALL QGraphicsItems must have a scene before they are deleted. +## This is potentially very expensive, but preferred over crashing. +import atexit +def cleanup(): + if QtGui.QApplication.instance() is None: + return + import gc + s = QtGui.QGraphicsScene() + for o in gc.get_objects(): + try: + if isinstance(o, QtGui.QGraphicsItem) and o.scene() is None: + s.addItem(o) + except RuntimeError: ## occurs if a python wrapper no longer has its underlying C++ object + continue +atexit.register(cleanup) + + ## Convenience functions for command-line use - - plots = [] images = [] QAPP = None @@ -176,8 +192,8 @@ show = image ## for backward compatibility def mkQApp(): + global QAPP if QtGui.QApplication.instance() is None: - global QAPP QAPP = QtGui.QApplication([]) - + return QAPP