Difference between revisions of "VTK/Tutorials/SmartPointers"

From KitwarePublic
< VTK‎ | Tutorials
Jump to navigationJump to search
(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...)
 
Line 21: Line 21:
  
 
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?!
 
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?!
 +
 +
Here is an example of equivalent operations with and without smart pointers:
 +
== SmartPointers.cpp ==
 +
<source lang="cpp">
 +
#include <vtkFloatArray.h>
 +
#include <vtkSmartPointer.h>
 +
 +
#include <iostream>
 +
 +
void WithSmartPointers();
 +
void WithoutSmartPointers();
 +
 +
int main(int argc, char *argv[])
 +
{
 +
  WithSmartPointers();
 +
  WithoutSmartPointers();
 +
 +
return 0;
 +
}
 +
 +
void WithSmartPointers()
 +
{
 +
  vtkSmartPointer<vtkFloatArray> Distances = vtkSmartPointer<vtkFloatArray>::New();
 +
}
 +
 +
void WithoutSmartPointers()
 +
{
 +
  vtkFloatArray* Distances = vtkFloatArray::New();
 +
  Distances->Delete();
 +
}
 +
 +
</source>
 +
 +
== CMakeLists.txt ==
 +
<source lang="text">
 +
cmake_minimum_required(VERSION 2.6)
 +
 +
PROJECT(SmartPointers)
 +
 +
FIND_PACKAGE(VTK REQUIRED)
 +
INCLUDE(${VTK_USE_FILE})
 +
 +
ADD_EXECUTABLE(SmartPointers SmartPointers.cpp)
 +
 +
TARGET_LINK_LIBRARIES(SmartPointers vtkHybrid)
 +
 +
</source>

Revision as of 08:14, 20 October 2009

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

Here is an example of equivalent operations with and without smart pointers:

SmartPointers.cpp

#include <vtkFloatArray.h>
#include <vtkSmartPointer.h>

#include <iostream>

void WithSmartPointers();
void WithoutSmartPointers();

int main(int argc, char *argv[])
{
  WithSmartPointers();
  WithoutSmartPointers();
	
	return 0;
}

void WithSmartPointers()
{
  vtkSmartPointer<vtkFloatArray> Distances = vtkSmartPointer<vtkFloatArray>::New();
}

void WithoutSmartPointers()
{
  vtkFloatArray* Distances = vtkFloatArray::New();
  Distances->Delete();
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)

PROJECT(SmartPointers)

FIND_PACKAGE(VTK REQUIRED)
INCLUDE(${VTK_USE_FILE})

ADD_EXECUTABLE(SmartPointers SmartPointers.cpp)

TARGET_LINK_LIBRARIES(SmartPointers vtkHybrid)