[vtkusers] clarification on smart pointer usage
Richard Frank
rickfrank at me.com
Wed Jan 2 18:01:12 EST 2013
Ah i see, thanks. I wasn't using the smart pointer template ::New().
Rick Frank
On Jan 2, 2013, at 5:08 PM, David Lonie <david.lonie at kitware.com> wrote:
> On Fri, Dec 28, 2012 at 11:57 PM, Richard Frank <rickfrank at me.com> wrote:
>> Thanks- I'm doing that. Given two smart pointers allocated with ::New() macro, what should happen to the lhs on assignment?
>
> If I remember correctly, this is what's happening:
>
> >> vtkSmartPointer<vtkVolumeRayCastCompositionFunction> one = vtkVolumeRayCastCompositionFunction::New();
>
> one's reference count is 2 -- once from vtkVolumeRayCastCompositionFunction::New(), and again from vtkSmartPointer::operator=().
>
> >> vtkSmartPointer<vtkVolumeRayCastCompositionFunction> two = vtkVolumeRayCastCompositionFunction::New();
>
> two's reference count is also 2.
>
> >> one = two;
>
> one should get decref'd to 1, while two gets increased to 3. Since one's refcount never gets to zero, the destructor never gets called.
>
> Using the method David mentioned:
>
>> > vtkSmartPointer<vtkVolumeRayCastCompositionFunction> one =
>> > vtkSmartPointer<vtkVolumeRayCastCompositionFunction>::New();
>
> one has 1 reference. You can check this by calling GetReferenceCount on the pointers.
>
> If you don't need to reassign the pointer, vtkNew is another useful smart pointer class that you may find useful. This article provides a nice short summary of these classes, and also vtkWeakPointer:
>
> http://www.kitware.com/source/home/post/7
>
> Hope this helps,
> Dave
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20130102/93f9128d/attachment.htm>
More information about the vtkusers
mailing list