@@ -78,7 +78,7 @@ def __init__(self, figure, num):
7878 self ._backend = get_backend ()
7979 self ._mainloop = self ._backend .MainLoop ()
8080 self .window = self ._backend .Window ('Figure %d' % num )
81- self .window .mpl_connect ('window_destroy_event' , self ._destroy )
81+ self .window .mpl_connect ('window_destroy_event' , self .destroy )
8282
8383 self .canvas = self ._backend .FigureCanvas (figure , manager = self )
8484
@@ -122,23 +122,29 @@ def key_press(self, event):
122122 """
123123 key_press_handler (event , self .canvas , self .canvas .toolbar )
124124
125- def _destroy (self , event = None ):
126- # Callback from the when the window wants to destroy itself
127- s = 'window_destroy_event'
128- event = FigureManagerEvent (s , self )
129- self ._callbacks .process (s , event )
130-
131125 def destroy (self , * args ):
132- """Called to destroy this FigureManager, gets called by Gcf through
133- event magic.
126+ """Called to destroy this FigureManager.
134127 """
128+
129+ # Make sure we run this routine only once for the FigureManager
130+ # This ensures the nasty __del__ fix below works.
131+ if getattr (self , '_destroying' , False ):
132+ return
133+
134+ self ._destroying = True
135135 self .canvas .destroy ()
136136 if self .toolbar :
137137 self .toolbar .destroy ()
138138 self .window .destroy ()
139139
140+ # Fix as for some reason we have extra references to this#
141+ # i.e. ``del self._mainloop`` doesn't work
140142 self ._mainloop .__del__ ()
141143
144+ s = 'window_destroy_event'
145+ event = FigureManagerEvent (s , self )
146+ self ._callbacks .process (s , event )
147+
142148 def show (self ):
143149 """Shows the figure"""
144150 self .window .show ()
0 commit comments