Skip to content

Commit

Permalink
fixed memory leak in quiverkey
Browse files Browse the repository at this point in the history
closes #2556
  • Loading branch information
tacaswell committed Jan 14, 2014
1 parent c312cca commit 4cc466e
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions lib/matplotlib/quiver.py
Expand Up @@ -220,9 +220,9 @@

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

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

# try to prevent closure over the real self
weak_self = weakref.ref(self)

def on_dpi_change(fig):
self.labelsep = (self._labelsep_inches * fig.dpi)
self._initialized = False # simple brute force update
# works because _init is called
# at the start of draw.
_s = weak_self()
if _s is not None:
_s.labelsep = (_s._labelsep_inches * fig.dpi)
_s._initialized = False # simple brute force update
# works because _init is called
# at the start of draw.

self._cid = Q.ax.figure.callbacks.connect('dpi_changed',
on_dpi_change)

Q.ax.figure.callbacks.connect('dpi_changed', on_dpi_change)
self._cb_ref = weakref.ref(Q.ax.figure.callbacks)

self.labelpos = kw.pop('labelpos', 'N')
self.labelcolor = kw.pop('labelcolor', None)
Expand All @@ -255,11 +263,24 @@ def on_dpi_change(fig):
horizontalalignment=self.halign[self.labelpos],
verticalalignment=self.valign[self.labelpos],
fontproperties=font_manager.FontProperties(**_fp))

if self.labelcolor is not None:
self.text.set_color(self.labelcolor)
self._initialized = False
self.zorder = Q.zorder + 0.1

def remove(self):
"""
Overload the remove method
"""
_cbs = self._cb_ref()
if _cbs is not None:
# disconnect the call back
_cbs.disconnect(self._cid)
self._cid = None
# pass the remove call up the stack
martist.Artist.remove(self)

__init__.__doc__ = _quiverkey_doc

def _init(self):
Expand Down

0 comments on commit 4cc466e

Please sign in to comment.