[vtk-developers] Patch for vtkPythonUtil.cxx

Chris Kruszynski Chris.Kruszynski at cwi.nl
Mon Aug 4 10:04:09 EDT 2008


On 04-08-08 11:33, Charl Botha wrote:
> 2008/8/3 Prabhu Ramachandran <prabhu at aero.iitb.ac.in>:
>   
>>> I ran into a bug that causes a crash on some VTK-Python apps at exit. It
>>> is hard to reproduce with a small test case but reproducible in some TVTK
>>> scripts I have.  Basically, what seems to happen is that if there is a VTK
>>> object on which you have a Python callback via an event notification, say
>>> "ModifiedEvent" or "DeleteEvent", then it is possible (with VTK 5.2/5.3) for
>>> the callback to be called *after* PyFinalize is called.  This will cause a
>>> segfault.  The patch just checks to see if the interpreter is initialized
>>> and if not return.  This avoids the problem.
>>>       
>
> Funny coincidence.  I just ran into this with a vtkProgrammableFilter.
> :)  Thanks for fixing.
> _______________________________________________
> vtk-developers mailing list
> vtk-developers at vtk.org
> http://www.vtk.org/mailman/listinfo/vtk-developers
>   
I have observed this problem some time ago, it would actually need a 
second patch to fix all occurrences (or just another one?), which I have 
attached.

The test case is easy:

#!/usr/bin/env vtkpython

import vtk

class TestCase :
  def __init__(self) :
    self.Object = vtk.vtkObject()
    self.Object.AddObserver('StartEvent', self.Handler)
  def Handler(self, obj, evt) :
    print 'event received'

test = TestCase()

This should cause a crash upon termination (trying to delete python 
object after interpreter shutdown), or an assertion error if the 
observed event is 'DeleteEvent' or 'AnyEvent' (trying to invoke python 
method after interpreter shutdown). The test case basically consists of 
a python class which contains a vtk object which is being observed by 
that same class. There is no error if the event handler is a member of a 
different class, even if the instance of that class is contained by the 
same python object which contains the vtk object. There is also no error 
if a vtk object is subclassed in python and that subclass has an event 
handler for itself (self.AddObserver(Event, self.Handler)). Finally, the 
problem disappears if the container class has a cyclic reference to 
itself (self.CyclicReference = self).

The problem can actually be pretty widespread since it affects all 
python classes which both contain and observe a vtk object.

Before the changes to reference counting in revision 1.84 of 
vtkPythonUtil.cxx, the problem was masked, because an extra reference to 
the vtk object would remain somewhere, and the vtk object would never be 
deleted (and thus RemoveAllObservers would never be called, and 
vtkPythonCommand would never try to delete the python callable object 
after interpreter shutdown).


regards,

Chris Kruszynski
-------------- next part --------------
A non-text attachment was scrubbed...
Name: pyutilfix.patch
Type: text/x-patch
Size: 474 bytes
Desc: not available
URL: <http://public.kitware.com/pipermail/vtk-developers/attachments/20080804/f7928772/attachment-0001.bin>


More information about the vtk-developers mailing list