[vtkusers] About Pitfalls section in smart pointers tutorial
Marcus D. Hanwell
marcus.hanwell at kitware.com
Wed Jun 29 15:42:10 EDT 2016
On Wed, Jun 29, 2016 at 3:28 PM, Elvis Stansvik
<elvis.stansvik at orexplore.com> wrote:
> 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?
I have the same interpretation, it looks like it was added by David
Doria in 2009. It is silly to allocate a new object, and then
immediately point to something else without ever using that object,
but the reference count for that object should be correctly
decremented and so deleted as far as I can see.
I am not sure the pitfalls section has any real value as the only
remaining point is quite generically true, don't create objects you
are not going to use (on the stack, pointers, or smart pointers).
Anyone see something I am missing there?
More information about the vtkusers