[vtkusers] vtkSmartPointer usage question

Alex Malyushytskyy alexmalvtk at gmail.com
Fri Nov 6 18:03:21 EST 2009


Jeff,

> sp = NULL; // Release would happen here.
That was what I was looking for.
When I tried to look at the code I forgot that vtkSmartPointer is
dialing with vtkObjectBase pointers.

> Normally you just wait for the smart pointer to go out of scope.

Main reason for me to use smartpointers - it helps dealing with
exceptions especially thrown from constructors.
So I usually use smart pointers as a class members instead of raw pointers .
And even though smartpointer will do his job when it goes out of scope,
you may get situation when you need to replace the data when it does not.

Example:
Assume poly data is read from the file and stored with smartpointer.
User action changes the data (for example some additional tessellation).
If result is satisfactory, old data should be replaced.

Thanks everybody for help,
             Alex




On Fri, Nov 6, 2009 at 5:53 AM, Jeff Baumes <jeff.baumes at kitware.com> wrote:
>> The only thing is  not clear  for me is how to force smartpointer to
>> release the data.
>>
>
> Is this what you're looking for?
> vtkSmartPointer<vtkType> sp = vtkSmartPointer<vtkType>::New();
> ...
> sp = NULL; // Release would happen here.
> This would decrement the reference count of the object by one. If no one
> else holds a reference count of the object (i.e. reference count goes to
> zero), it will be released from memory.
> Normally you just wait for the smart pointer to go out of scope. If you
> explicitly need to make the reference go away at a certain point, you might
> as well not use smart pointers and just do this equivalent code:
> vtkType* p = vtkType::New();
> ...
> p->Delete();
> Jeff



More information about the vtkusers mailing list