[vtkusers] Destructor and vector

Sean McInerney seanm at nmr.mgh.harvard.edu
Thu Sep 30 23:31:31 EDT 2004


Hi Jerome,

Let me apologize in advance for the overly nerdy explanation that 
follows ... but I believe that it's correct ... maybe it's a Wiki 
candidate???

The STL provides value semantics while VTK objects are dealt with 
exclusively via reference semantics (VTK enforces this by leaving the 
copy constructor and assignment operator of its objects unimplemented). 
Thankfully, VTK already provides vtkSmartPointer for use with such 
templated container types. It is a "wrapper class" that implements 
reference semantics for the STL and similar containers. Basically, 
vtkSmartPointer grafts VTK's reference counting scheme into templated 
container classes (list, vector, stack, etc.) so that vtkObjects can be 
dealt with just as they are in a vtkCollection. vtkSmartPointer assures 
that a vtkObject's reference count is incremented upon its addition to a 
container and decremented on removal. Proper use of vtkSmartPointer 
makes it easier to prevent an object's reference count being decremented 
to zero (and its subsequent destruction) happening while a pointer to 
that object is still held within a container.

So ... let's say that you want a vector of vtkPlaneSources ...

// Handy typedefs for brevity (vector is in the vtkstd:: namespace)
typedef vtkSmartPointer<vtkPlaneSource>    vtkPlaneSourceRef;
typedef vtkstd::vector<vtkPlaneSourceRef>  vtkPlaneSourceVector;

{
   // Create the container.
   vtkPlaneSourceVector vec;
   // Create objects destined for the container.
   vtkPlaneSource* planeSource1 = vtkPlaneSource::New();
   vtkPlaneSource* planeSource2 = vtkPlaneSource::New();

   // Push the objects into the container wrapped in smart pointers.
   vec.push_back(vtkPlaneSourceRef(planeSource1));
   vec.push_back(vtkPlaneSourceRef(planeSource2));

   // Decrement the sources' ref counts ... the vector now "owns" them!
   planeSource1->Delete(); // Ref count is now 1.
   planeSource2->Delete(); // Ref count is now 1.

   // Do something ... like iterate over the container ...
   for ( vtkPlaneSourceVector::iterator pos = vec.begin();
         pos != vec.end(); ++pos )
     {
     // Yeah, I know the added indirection of 'GetPointer()' is
     // ugly, but it's a small price to pay. If you're STL-savvy,
     // you've probably dispensed with petty aesthetic
     // considerations anyway.
     pos->GetPointer()->Print(cerr);
     }

   // Pop the plane source from the back of the container.
   vec.pop_back(); // 'planeSource2' destructs upon its removal.

   // 'vec' destructs as it goes out of scope *AND* so does
   // 'planeSource1' which is still held by the container!
   // No loose ends to worry about!
}


Ah yes, working out my presidential debate frustrations with a little 
soothing VTK fun.

-Sean

Charles Boivin wrote:
> Bonjour Jerome,
> 
> Although I have never done it with vtkPlaneSource specifically, I use
> the STL with VTK objects... For example, you could have a vector of
> pointers to vtkPlaneSource objects like so:
> 
> std::vector< vtkPlaneSource* > vecPlaneSources;
> 
> Just make sure to instantiate things properly with the
> vtkPlaneSource::New() function before pushing them on the vector, and
> also, remember to call vtkPlaneSource::Delete() for each item in the
> vector before clearing it. 
> 
> I do just that with other VTK objects.. works fine for me. I'd be
> interested to hear if there are any potential dangers associated with
> this use of the STL from some of the VTK and C++ gurus on this mailing
> list, however.
> 
> Hope that helps,
> 
> Charles Boivin
> 
> 
>>>>Jérôme Lecoq <jerome.lecoq at espci.fr> 09/30/04 08:52am >>>
> 
> I guess I found myself the answear.
> the vector are simply not implemented in vtk... But will be in the
> future
> releases.
> 
> http://public.kitware.com/cgi-bin/vtkfaq?req=show&file=faq06.011.htp 
> :
> "Further integration of STL and other important C++ constructs (like
> templates)" in Roadmap: What changes are being considered for VTK
> 
> and in vtkPlaneSource.h
> "
> vtkPlaneSource(const vtkPlaneSource&);  // Not implemented.
>   void operator=(const vtkPlaneSource&);  // Not implemented."
> 
> declaration which are needed to use vector, list, stack and so on...
> 
> I'll have to find another solution...




More information about the vtkusers mailing list