2

I am using VTK for Visualization and my code is full of their smartpointers, like:

vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();

what I was wondering about if this New() should not be followed later by Delete(). Or does VTK destroy everything "automatically". Many times by using Delete() my code crashes. So, I was wondering if I should use it in first place and what is behind New(), a shared pointer or something like this?

4

2 に答える 2

9

vtkSmartPointer will destroy the object "automatically" when out of scope. The Delete() method is called in the dtor of it.

Without vtkSmartPointer, you need to take care the memory management issues by yourself and call Delete(), like following

vtkObject* MyObject = vtkObject::New();
MyObject->Delete();

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

See the wiki page: http://www.vtk.org/Wiki/VTK/Tutorials/SmartPointers

于 2012-08-22T07:33:07.670 に答える
6

Another alternative to

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

is

vtkNew<vtkObject> MyObject;

Just note that when passing MyObject to functions/methods that take in a vtkObject*, you have to use MyObject.GetPointer() e.g.

foo->SetObject(MyObject.GetPointer());
于 2013-08-01T00:00:34.237 に答える