[vtkusers] clarification on smart pointer usage

David Gobbi david.gobbi at gmail.com
Fri Dec 28 22:27:41 EST 2012


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