[vtkusers] clarification on smart pointer usage

Richard Frank rickfrank at me.com
Fri Dec 28 23:57:53 EST 2012


Thanks- I'm doing that. Given two smart pointers allocated with ::New() macro, what should happen to the lhs on assignment?

i.e 

lhs = rhs;

What I seemed to see was lhs leaking- destructor not called.

I would imagine that ref count would be 1 on creation, then decremented on assignment, and delete called if ref count is 0.....



Rick Frank

On Dec 28, 2012, at 10:27 PM, David Gobbi <david.gobbi at gmail.com> wrote:

> Hi Frank,
> 
> Here is the correct code for assigning a smart pointer to a new object:
> 
> vtkSmartPointer<vtkVolumeRayCastCompositionFunction> one =
>  vtkSmartPointer<vtkVolumeRayCastCompositionFunction>::New();
> 
> You need to use the special smart pointer "New" method to create the
> new object.  Otherwise, the object ends up with a reference count of
> two (one from the New and one from assigning the smart pointer).
> 
> - David
> 
> 
> On Fri, Dec 28, 2012 at 6:35 PM, Rick Frank <rickfrank at me.com> wrote:
>> 
>> Hi,
>> 
>> 
>> Tracing through the code seems to indicate that assigning one smart point to another does not free the left hand side pointer.
>> 
>> For instance,
>> 
>> vtkSmartPointer<vtkVolumeRayCastCompositionFunction> one = vtkVolumeRayCastCompositionFunction::New();
>> 
>> 
>> vtkSmartPointer<vtkVolumeRayCastCompositionFunction> two = vtkVolumeRayCastCompositionFunction::New();
>> 
>> 
>> one = two;
>> 
>> // The pointer in one seems to be left undeleted?
>> 
>> Is there a standard best-pattern for  deleting the left hand side of the assignment?
>> 
>> 
>> The docs say the reference to the pointer is removed, but that's a bit unclear to me.
>> 
>> In the above, I put a break point on the dtor of vtkVolumeRayCastCompositionFunction and it wasn't hit.
>> 
>> Thanks
>> 
>> Rick



More information about the vtkusers mailing list