VTK/Tutorials/SmartPointers

From KitwarePublic
< VTK‎ | Tutorials
Revision as of 16:05, 19 October 2009 by Daviddoria (talk | contribs) (New page: One way to create a VTK object is <source lang="cpp"> vtkObject* MyObject = vtkObject::New(); </source> This method, however, can (and likely will) lead to memory management issues at som...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

One way to create a VTK object is

vtkObject* MyObject = vtkObject::New();

This method, however, can (and likely will) lead to memory management issues at some point or another. You must manually delete the object

MyObject->Delete();

or you will have a memory leak. VTK's solution to this ever-annoying problem is the smart pointer. To use it, you must

#include "vtkSmartPointer.h"

Then you can create an object as follows:

vtkSmartPointer<vtkObject> MyObject = vtkSmartPointer<vtkObject>::New();

The idea behind smart pointers is reference counting. If the object goes out of scope and it is not being used anywhere else, it will be deleted automatically. Pretty 'smart', eh?!