VTK/Tutorials/SmartPointers

From KitwarePublic
< VTK‎ | Tutorials
Revision as of 21: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 <source lang="cpp"> vtkObject* MyObject = vtkObject::New(); </source>

This method, however, can (and likely will) lead to memory management issues at some point or another. You must manually delete the object <source lang="cpp"> MyObject->Delete(); </source>

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

<source lang="cpp">

  1. include "vtkSmartPointer.h"

</source>

Then you can create an object as follows: <source lang="cpp"> vtkSmartPointer<vtkObject> MyObject = vtkSmartPointer<vtkObject>::New(); </source>

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?!