<div dir="ltr"><div><div><div><div><div><div><div><div><div><div><div>Hi all,<br><br></div>Sorry if this is an obvious question, but I could only find a few mails in the archive about Python memory management and VTK.<br><br></div>My question is when I need to be weary of the Python garbage collector when setting up a VTK pipeline.<br><br></div>Consider the cone example from QVTKRenderWindowInteractor:<br><br><br>def QVTKRenderWidgetConeExample():<br>    """A simple example that uses the QVTKRenderWindowInteractor class."""<br><br>    # every QT app needs an app<br>    app = QApplication(['QVTKRenderWindowInteractor'])<br><br>    # create the widget<br>    widget = QVTKRenderWindowInteractor()<br>    widget.Initialize()<br>    widget.Start()<br>    # if you dont want the 'q' key to exit comment this.<br>    widget.AddObserver("ExitEvent", lambda o, e, a=app: a.quit())<br><br>    ren = vtk.vtkRenderer()<br>    widget.GetRenderWindow().AddRenderer(ren)<br><br>    cone = vtk.vtkConeSource()<br>    cone.SetResolution(8)<br><br>    coneMapper = vtk.vtkPolyDataMapper()<br>    coneMapper.SetInputConnection(cone.GetOutputPort())<br><br>    coneActor = vtk.vtkActor()<br>    coneActor.SetMapper(coneMapper)<br><br>    ren.AddActor(coneActor)<br><br>    # show the widget<br>    widget.show()<br>    # start event processing<br>    app.exec_()<br><br><br></div>In this example, the program will block on the app.exec_() call, while still inside the QVTKRenderWidgetConeExample() and with all the created variables thus "alive". So no fear of them being garbage collected.<br><br></div>But what if I instead moved this pipeline setup code into the __init__ of a QVTKRenderWindowInteractor subclass, like this:<br><br><br>class MyWidget(QVTKRenderWindowInteractor):<br>    def __init__(self, *args, **kwargs):<br>        super(MyWidget, self).__init__(*args, **kwargs)<br><br>        self.Initialize()<br>        self.Start()<br><br>        ren = vtkRenderer()<br>        self.GetRenderWindow().AddRenderer(ren)<br><br>        cone = vtkConeSource()<br>        cone.SetResolution(8)<br><br>        coneMapper = vtkPolyDataMapper()<br>        coneMapper.SetInputConnection(cone.GetOutputPort())<br><br>        coneActor = vtkActor()<br>        coneActor.SetMapper(coneMapper)<br><br>        ren.AddActor(coneActor)<br><br><br></div>This works, Python is not performing garbage collection of anything, despite me not keeping the involved variables (renderer, source, mapper, actor) as fields of my class. I suppose this is because a reference to the renderer is kept in the render window, which is kept in the base class, and the renderer holds the actor, which hoids the mapper, which holds the output port of the source. So nothing is eligible for garbage collection.<br><br></div>So my question is, can I always rely on this behavior when working with pipelines through the Python wrappers? Or are there cases where VTK will do things in C++ "behind the back" of the Python garbage collector?<br><br></div>The ultra-safe way is of course to keep explicit references to everything:<br><br><br>class MyWidget(QVTKRenderWindowInteractor):<br>    def __init__(self, *args, **kwargs):<br>        super(MyWidget, self).__init__(*args, **kwargs)<br><br>        self.Initialize()<br>        self.Start()<br><br>        self.ren = vtkRenderer()<br>        self.GetRenderWindow().AddRenderer(self.ren)<br><br>        self.cone = vtkConeSource()<br>        self.cone.SetResolution(8)<br><br>        self.coneMapper = vtkPolyDataMapper()<br>        self.coneMapper.SetInputConnection(self.cone.GetOutputPort())<br><br>        self.coneActor = vtkActor()<br>        self.coneActor.SetMapper(self.coneMapper)<br><br>        self.ren.AddActor(self.coneActor)<br><br><br></div>And in fact, most of the time you probably want to keep references to a lot of things, to be able to reconfigure the pipeline. I'm just wondering if there are any gotchas to watch out for wrt to garbage collection when not keeping references. If there are any "best practices" so to speak.<br><br></div>Thanks in advance,<br></div>Elvis<br></div>