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

David Gobbi dgobbi at atamai.com
Sun Nov 6 19:16:34 EST 2005


I was looking for the answer in the wrong place.  All IGSTK needs
is a "New" macro for VTK-derived IGSTK classes that behaves like
the ITK "New" method, i.e. it should return a smart pointer instead
of a regular pointer:

#define igstkVTKNewMacro(myClass) \
static vtkSmartPointer<myClass> New(void) \
{ \
  return vtkSmartPointer<myClass>::Take(new myClass); \
}

My idea of creating a special smart pointer class was way out in
left field.


David Gobbi wrote:

> 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
>
>
> _______________________________________________
> IGSTK-Developers mailing list
> IGSTK-Developers at public.kitware.com
> http://public.kitware.com/cgi-bin/mailman/listinfo/igstk-developers
>




More information about the IGSTK-Developers mailing list