Discussion:
[Matplotlib-users] Avoid circular references with format_coord and Qt?
Sebastian Berg
2015-03-08 19:00:37 UTC
Permalink
Hey,

probably there is nothing to help me here, but I have a QtGui, using
matplotlib and basically end up in a circular reference hell that is not
resolvable by the gc. After removing almost all circular references, the
python inherited QWidget that embeds the figurecanvas is now deleted
fine.

The thing I end up with however is something like

class Visualize(object):
def __init__(self):
self.figure = Figure()
canvas = FigureCanvas(self.figure)
PythonInheritedQtWidget(self).element.setWidget(canvas)

def format(*args):
# do stuff
return something
self.axis = self.figure.add_subplot(1, 1, 1)
self.axis.format_coord = format

# This was completely drawn with canvas and all
# PythonInheritedQtWidget is deleted fine.

I have not been able to get a minimal test case. But I have it pinned
down that everything can get cleaned up after:

del Visualize.axes.format_coord

so the circular reference that makes it not collectable is only the
format_coord which is a method pointing to the class owning the figure
and the axis. I am about to just use a weak reference wrapper function
to break the circle but it seems silly.

Anyway, I do not expect that anyone can help, and I expect it is some Qt
stuff really, but this is seriously annoying me....

- Sebastian
Thomas Caswell
2015-03-08 19:44:15 UTC
Permalink
My knee-jerk reaction is to move the definition of `format` outside of the
`Visualize.__init__` method. If you need to have hooks back into the
visualize method I would use a function factory + weak refs or a class
(again with weakrefs) with a `__call__` method.

Tom
Post by Sebastian Berg
Hey,
probably there is nothing to help me here, but I have a QtGui, using
matplotlib and basically end up in a circular reference hell that is not
resolvable by the gc. After removing almost all circular references, the
python inherited QWidget that embeds the figurecanvas is now deleted
fine.
The thing I end up with however is something like
self.figure = Figure()
canvas = FigureCanvas(self.figure)
PythonInheritedQtWidget(self).element.setWidget(canvas)
# do stuff
return something
self.axis = self.figure.add_subplot(1, 1, 1)
self.axis.format_coord = format
# This was completely drawn with canvas and all
# PythonInheritedQtWidget is deleted fine.
I have not been able to get a minimal test case. But I have it pinned
del Visualize.axes.format_coord
so the circular reference that makes it not collectable is only the
format_coord which is a method pointing to the class owning the figure
and the axis. I am about to just use a weak reference wrapper function
to break the circle but it seems silly.
Anyway, I do not expect that anyone can help, and I expect it is some Qt
stuff really, but this is seriously annoying me....
- Sebastian
------------------------------------------------------------
------------------
Dive into the World of Parallel Programming The Go Parallel Website,
sponsored
by Intel and developed in partnership with Slashdot Media, is your hub for
all
things parallel software development, from weekly thought leadership blogs
to
news, videos, case studies, tutorials and more. Take a look and join the
conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Matplotlib-users mailing list
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Loading...