<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">2016-06-29 21:42 GMT+02:00 Marcus D. Hanwell <span dir="ltr"><<a href="mailto:marcus.hanwell@kitware.com" target="_blank">marcus.hanwell@kitware.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On Wed, Jun 29, 2016 at 3:28 PM, Elvis Stansvik<br>
<<a href="mailto:elvis.stansvik@orexplore.com">elvis.stansvik@orexplore.com</a>> wrote:<br>
> At<br>
><br>
>    <a href="http://www.vtk.org/Wiki/VTK/Tutorials/SmartPointers#Pitfalls" rel="noreferrer" target="_blank">http://www.vtk.org/Wiki/VTK/Tutorials/SmartPointers#Pitfalls</a><br>
><br>
> is this warning:<br>
><br>
> "If you create an object and then change where it is pointing, the reference<br>
> count will be incorrect. e.g.<br>
><br>
> vtkSmartPointer<vtkPolyData> Polydata = vtkSmartPointer<vtkPolyData>::New();<br>
> Polydata = Reader->GetOutput();<br>
><br>
> In this case, memory is allocated for Polydata, but then we change Polydata<br>
> to point to the output of Reader rather than the memory we just allocated.<br>
> Instead, we should have done simply:<br>
><br>
> vtkPolyData* Polydata = Reader->GetOutput();<br>
><br>
> It was not necessary to use a smart pointer because we did not actually<br>
> create any new objects"<br>
><br>
> I fail to see how the refcount would be incorrect in the first approach.<br>
> First a new object is created and its refcount increased to 1, then when<br>
> Reader->GetOutput() (a raw pointer) is assigned to Polydata, wouldn't the<br>
> smart pointer take care of decreasing the refcount for the object it already<br>
> points to (so it would go to 0), before it starts managing the new pointer?<br>
> Or is that not how it will work?<br>
><br>
</span>I have the same interpretation, it looks like it was added by David<br>
Doria in 2009. It is silly to allocate a new object, and then<br>
immediately point to something else without ever using that object,<br>
but the reference count for that object should be correctly<br>
decremented and so deleted as far as I can see.<br></blockquote><div><br></div><div>Yes, I forgot to say: I of course also think it's silly, I was just curious about the wording of that warning since it didn't make sense to me.<br><br></div><div>Elvis<br> <br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
I am not sure the pitfalls section has any real value as the only<br>
remaining point is quite generically true, don't create objects you<br>
are not going to use (on the stack, pointers, or smart pointers).<br>
<br>
Anyone see something I am missing there?<br>
<br>
Thanks,<br>
<br>
Marcus<br>
</blockquote></div><br></div></div>