[IGSTK-Developers] vtkSmartPointer vs. itk::SmartPointer

David Gobbi dgobbi at atamai.com
Sat Nov 5 12:53:15 EST 2005


Hi All,

My idea of using smart pointers for IGSTK objects that are derived from VTK
objects has hit a snag. I think that I have a solution, but I would like to
hear opinions of people who have experience with mixing VTK and ITK first.

The way that new objects are created with a smart pointer is differnt in VTK
as compared to ITK.  In ITK, the object's New() method is called, but in 
ITK,
the smart pointer's New() method must be called:

  itk::SmartPointer<SomeITKObject> pointer = itk::SomeITKObject::New();

vs.

  vtkSmartPointer<SomeVTKObject> pointer = 
vtkSmartPointer<SomeVTKObject>::New();

The biggest problem here is that if people mistakenly use ITK-style object
creation with a VTK object, a memory leak occurs:

void someFunction( void )
{
  vtkSmartPointer<SomeVTKObject> pointer = SomeVTKObject::New();
}
... memory leak, should have used vtkSmartPointer<SomeVTKObject>::New();


What I am thinking of doing is creating a igstk::VTKSmartPointer template,
that acts as a smart pointer for VTK objects, but which follows ITK 
semantics.
That way, the "igstk::View::Pointer" typedef will act just like a ::Pointer
for any of the other IGSTK objects.

Does this sound reasonable?

 - David





More information about the IGSTK-Developers mailing list