[vtkusers] About Pitfalls section in smart pointers tutorial

Elvis Stansvik elvis.stansvik at orexplore.com
Wed Jun 29 15:28:10 EDT 2016



is this warning:

"If you create an object and then change where it is pointing, the
reference count will be incorrect. e.g.

vtkSmartPointer<vtkPolyData> Polydata = vtkSmartPointer<vtkPolyData>::New();
Polydata = Reader->GetOutput();

In this case, memory is allocated for Polydata, but then we change Polydata
to point to the output of Reader rather than the memory we just allocated.
Instead, we should have done simply:

vtkPolyData* Polydata = Reader->GetOutput();

It was not necessary to use a smart pointer because we did not actually
create any new objects"

I fail to see how the refcount would be incorrect in the first approach.
First a new object is created and its refcount increased to 1, then when
Reader->GetOutput() (a raw pointer) is assigned to Polydata, wouldn't the
smart pointer take care of decreasing the refcount for the object it
already points to (so it would go to 0), before it starts managing the new
pointer? Or is that not how it will work?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20160629/6bad6151/attachment.html>

More information about the vtkusers mailing list