Cannot delete matplotlib.animation.FuncAnimation objects

EDIT / TL; DR: There seems to be an object matplotlib.backends.backend_qt4.TimerQTthat contains a link to my FuncAnimation object. How to delete it to free a FuncAnimation object?

1 - Small context

I am trying to animate a plot created with matplotlib. I am using matplotlib.animation.FuncAnimation. This animated plot is contained in FigureCanvasQTAgg (matplotlib.backends.backend_qt4agg), i.e. PyQt4 widget.

class ViewerWidget(FigureCanvasQTAgg):
    def __init__(self, viewer, parent):
        # viewer is my FuncAnimation, encapsulated in a class
        self._viewer = viewer
        FigureCanvasQTAgg.__init__(self, viewer.figure)

When a configuration change occurs in the graphical interface, the drawing is cleared ( figure.clf()), and its subheadings (axes and lines) are replaced with new ones.

2 - Source code from the class Viewer(encapsulation FuncAnimation)

This is the most important part of my method Viewer.show(...)that initializes FuncAnimation.

2.a - :

animation.FuncAnimation(..., blit=True)

,

2.b - :

self._anim = animation.FuncAnimation(..., blit=True)

, ,

2.c - , del:

# Delete previous FuncAnimation if any       
if self._anim:
    del self._anim
self._anim = animation.FuncAnimation(..., blit=True)

2.d - :

# DEBUG: check garbage collector
def objects_by_id(id_):
    for obj in gc.get_objects():
        if id(obj) == id_:
            return obj
    self._id.remove(id_)
    return "garbage collected"

# Delete previous FuncAnimation if any       
if self._anim:
    del self._anim

# DEBUG
print "-"*10
for i in self._id.copy():
    print i, objects_by_id(i)
print "-"*10
self._anim = animation.FuncAnimation(self._figure_handler.figure,
                                     update,
                                     init_func=init,
                                     interval=self._update_anim,
                                     blit=True)

# DEBUG: store ids only, to enable object being garbage collected
self._anim_id.add(id(anim))

:

----------
140488264081616 <matplotlib.animation.FuncAnimation object at 0x7fc5f91360d0>
140488264169104 <matplotlib.animation.FuncAnimation object at 0x7fc5f914b690>
140488145151824 <matplotlib.animation.FuncAnimation object at 0x7fc5f1fca750>
140488262315984 <matplotlib.animation.FuncAnimation object at 0x7fc5f8f86fd0>
----------

, , FuncAnimation

2.e - , weakref:

# DEBUG: check garbage collector
def objects_by_id(id_):
    for obj in gc.get_objects():
        if id(obj) == id_:
            return obj
    self._id.remove(id_)
    return "garbage collected"

# Delete previous FuncAnimation if any
if self._anim_ref:
    anim = self._anim_ref()
    del anim


# DEBUG
print "-"*10
for i in self._id.copy():
    print i, objects_by_id(i)
print "-"*10
anim = animation.FuncAnimation(self._figure_handler.figure,
                               update,
                               init_func=init,
                               interval=self._update_anim,
                               blit=True)

self._anim_ref = weakref.ref(anim)

# DEBUG: store ids only, to enable object being garbage collected
self._id.add(id(anim))

, , , .

----------
140141921353872 <built-in method alignment>
----------
----------
140141921353872 <built-in method alignment>
140141920643152 Bbox('array([[ 0.,  0.],\n       [ 1.,  1.]])')
----------
----------
140141921353872 <built-in method alignment>
140141920643152 <viewer.FftPlot object at 0x7f755565e850>
140141903645328 Bbox('array([[ 0.,  0.],\n       [ 1.,  1.]])')
----------
(...)

<matplotlib.animation.FuncAnimation object at 0x...>?

, , ... FuncAnimation "". "init". , FuncAnimation , Viewer.show(...), sinces anim .

3 -

, . ?

EDIT: objgraph, FuncAnimation, :

            import objgraph, time
            objgraph.show_backrefs([self._anim],
                                   max_depth=5,
                                   filename="/tmp/debug/func_graph_%d.png"
                                   % int(time.time()))

backlinks

, matplotlib.backends.backend_qt4.TimerQT, . ?

+2
1

, , , .

Animation, mpl draw_event, Animation ( ) mpl close_event, .

mpl , weakref . , , , , mpl , .

, , Qt , , ( ), Animation, , , , , ref . , , , .

, Animation, , , - , ( , ).

Animation , ( ) _stop, ( , close_event).

+2

All Articles