8000 Work around a huge memory leak in PySide on Python 3 · matplotlib/matplotlib@a798b16 · GitHub
[go: up one dir, main page]

Skip to content

Commit a798b16

Browse files
committed
Work around a huge memory leak in PySide on Python 3
1 parent cd84c99 commit a798b16

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

lib/matplotlib/backends/backend_qt4agg.py

Lines changed: 12 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,11 @@ 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+
del qImage
118+
if refcnt != sys.getrefcount(stringBuffer):
119+
# Fix a huge memory leak in PySide on Python 3
120+
_decref(stringBuffer)
109121
else:
110122
bbox = self.blitbox
111123
l, b, r, t = bbox.extents

0 commit comments

Comments
 (0)
0