Python problems due to global vars in shared libs
Problems can arise when there are multiple copies of VTK on a system, and they are both loaded into Python at the same time. This was recently illustrated by !8853 (closed).
For example, let's say you have one copy of VTK that has been installed, and another copy in your build tree. And then, let's say you created your own custom VTK module called mymodules.vtkMyFirstModule.
import mymodules.vtkMyFirstModule
This will implicitly import vtkmodules.vtkCommonCore
as well, via the dependency chain. But depending on which copy of vtkmodules.vtkCommonCore
the Python module loader finds, we might end up with a situation like this:
mymodules.vtkMyFirstModule -> dynamically links to VTK shared libs in the build tree
vtkmodules.vtkCommonCore -> dynamically links to VTK shared libs in the installed VTK
The problem with this situation is that the VTK shared libs will be loaded twice, once from each location. And each copy of each shared lib will have its own data segment with its own globals.
One such global variable is located in libvtkWrappingPythonCore
, in vtkPythonUtil.cxx
:
static vtkPythonUtil* vtkPythonMap;
This variable holds mappings that are central to the VTK Python wrappers, and it must be unique and properly initialized or else the wrappers will behave very badly. It's not the only global variable, but it's the one most important for the wrappers.
Edit: The Python types (PyVTKMethodDescriptor_Type
etc.) that are defined in libvtkWrappingPythonCore
are also globals. They should be placed in a Python extension module (perhaps in vtkCommonCore.so/.pyd
), not in a shared library that can be linked by several modules.