[vtkusers] vtkDenseArray reference counting?
Jeff Baumes
jeff.baumes at kitware.com
Mon Nov 16 09:39:58 EST 2009
> If I create a vtkDenseArray of pointers, then create a smart pointer to an
> object, store it in the dense array, then let the smart pointer go out of
> scope, shouldn't it know that it is still needed in the dense array so it
> should not delete the memory? I looked at the reference count and it does
> not increase when the object is added to the array with SetValue.
> vtkSmartPointer<vtkDenseArray<vtkRay*> > array =
> vtkSmartPointer<vtkDenseArray<vtkRay*> >::New();
As your code is written now, you are telling the array to store raw
pointers; it does not even know that the pointers are vtkObject
subclasses, so it does not know it should increment the reference
count. The correct way to make this "just work" is to define the
storage container as
vtkSmartPointer<vtkDenseArray<vtkSmartPointer<vtkRay> > > array = ...
Of course you may need to do some trickery for allowing
vtkSmartPointer<vtkRay> to be converted to a vtkVariant. Similarly, a
nice way to store vtkObject classes in a std::vector (or any other STL
storage container) is with:
std::vector<vtkSmartPointer<vtkRay> > array;
...
array[i] = vtkSmartPointer<vtkRay>::New();
This will automatically keep a reference count for each object in the
array until the array is destructed.
Jeff
More information about the vtkusers
mailing list