8000 Merge pull request #1323 from cgohlke/patch-13 · matplotlib/matplotlib@b6cf115 · GitHub
[go: up one dir, main page]

Skip to content

Commit b6cf115

Browse files
committed
Merge pull request #1323 from cgohlke/patch-13
Work around a huge memory leak in PySide on Python 3
2 parents 0b41db6 + 43e8be2 commit b6cf115

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

lib/matplotlib/backends/backend_qt4agg.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from __future__ import division, print_function
55

66
import os, sys
7+
import ctypes
78

89
import matplotlib
910
from matplotlib.figure import Figure
@@ -15,6 +16,10 @@
1516

1617
DEBUG = False
1718

19+
_decref = ctypes.pythonapi.Py_DecRef
20+
_decref.argtypes = [ctypes.py_object]
21+
_decref.restype = None
22+
1823

1924
def new_figure_manager( num, *args, **kwargs ):
2025
"""
@@ -95,6 +100,8 @@ def paintEvent( self, e ):
95100
else:
96101
stringBuffer = self.renderer._renderer.tostring_argb()
97102

103+
refcnt = sys.getrefcount(stringBuffer)
104+
98105
qImage = QtGui.QImage(stringBuffer, self.renderer.width,
99106
self.renderer.height,
100107
QtGui.QImage.Format_ARGB32)
@@ -106,6 +113,14 @@ def paintEvent( self, e ):
106113
p.setPen( QtGui.QPen( QtCore.Qt.black, 1, QtCore.Qt.DotLine ) )
107114
p.drawRect( self.rect[0], self.rect[1], self.rect[2], self.rect[3] )
108115
p.end()
116+
117+
# This works around a bug in PySide 1.1.2 on Python 3.x,
118+
# where the reference count of stringBuffer is incremented
119+
# but never decremented by QImage.
120+
# TODO: revert PR #1323 once the issue is fixed in PySide.
121+
del qImage
122+
if refcnt != sys.getrefcount(stringBuffer):
123+
_decref(stringBuffer)
109124
else:
110125
bbox = self.blitbox
111126
l, b, r, t = bbox.extents

0 commit comments

Comments
 (0)
0