Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4cc466e

Browse files
committed
fixed memory leak in quiverkey
closes matplotlib#2556
1 parent c312cca commit 4cc466e

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

lib/matplotlib/quiver.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,9 @@
220220

221221
class QuiverKey(martist.Artist):
222222
""" Labelled arrow for use as a quiver plot scale key."""
223-
halign = {'N': 'center', 'S': 'center', 'E': 'left', 'W': 'right'}
224-
valign = {'N': 'bottom', 'S': 'top', 'E': 'center', 'W': 'center'}
225-
pivot = {'N': 'mid', 'S': 'mid', 'E': 'tip', 'W': 'tail'}
223+
halign = {'N': 'center', 'S': 'center', 'E': 'left', 'W': 'right'}
224+
valign = {'N': 'bottom', 'S': 'top', 'E': 'center', 'W': 'center'}
225+
pivot = {'N': 'mid', 'S': 'mid', 'E': 'tip', 'W': 'tail'}
226226

227227
def __init__(self, Q, X, Y, U, label, **kw):
228228
martist.Artist.__init__(self)
@@ -236,13 +236,21 @@ def __init__(self, Q, X, Y, U, label, **kw):
236236
self._labelsep_inches = kw.pop('labelsep', 0.1)
237237
self.labelsep = (self._labelsep_inches * Q.ax.figure.dpi)
238238

239+
# try to prevent closure over the real self
240+
weak_self = weakref.ref(self)
241+
239242
def on_dpi_change(fig):
240-
self.labelsep = (self._labelsep_inches * fig.dpi)
241-
self._initialized = False # simple brute force update
242-
# works because _init is called
243-
# at the start of draw.
243+
_s = weak_self()
244+
if _s is not None:
245+
_s.labelsep = (_s._labelsep_inches * fig.dpi)
246+
_s._initialized = False # simple brute force update
247+
# works because _init is called
248+
# at the start of draw.
249+
250+
self._cid = Q.ax.figure.callbacks.connect('dpi_changed',
251+
on_dpi_change)
244252

245-
Q.ax.figure.callbacks.connect('dpi_changed', on_dpi_change)
253+
self._cb_ref = weakref.ref(Q.ax.figure.callbacks)
246254

247255
self.labelpos = kw.pop('labelpos', 'N')
248256
self.labelcolor = kw.pop('labelcolor', None)
@@ -255,11 +263,24 @@ def on_dpi_change(fig):
255263
horizontalalignment=self.halign[self.labelpos],
256264
verticalalignment=self.valign[self.labelpos],
257265
fontproperties=font_manager.FontProperties(**_fp))
266+
258267
if self.labelcolor is not None:
259268
self.text.set_color(self.labelcolor)
260269
self._initialized = False
261270
self.zorder = Q.zorder + 0.1
262271

272+
def remove(self):
273+
"""
274+
Overload the remove method
275+
"""
276+
_cbs = self._cb_ref()
277+
if _cbs is not None:
278+
# disconnect the call back
279+
_cbs.disconnect(self._cid)
280+
self._cid = None
281+
# pass the remove call up the stack
282+
martist.Artist.remove(self)
283+
263284
__init__.__doc__ = _quiverkey_doc
264285

265286
def _init(self):

0 commit comments

Comments
 (0)