[vtkusers] Destructor and vector

Charles Boivin Charles.Boivin at rwdiwest.com
Mon Oct 4 12:42:33 EDT 2004


Hello Sean,

Thanks for letting me in on this. I must admit that, aside from knowing
that VTK internally had a reference count and was enforcing that count
through the use of the New() and Delete() member functions, I had no
idea how everything was really working. Your explanation helps shed some
light on the topic.

I like the idea of an internal reference count... and I've always
disliked the fact that it was very easy to leak memory through the use
of the STL by forgetting to delete each pointer in an STL container
before clearing it. This is also sometimes very hard to track properly.
The example you gave with vtkSmartPointer pretty much gets rid of that.
Great stuff! I knew there must have been a safer way of doing things
that what I was doing.. Nice to know.

It's nice to be in touch with people who are obviously way more
experienced with C++ than myself. It's a great way to learn. Thanks a
lot for your time and effort. I hope it helped vent out your
frustrations.. ;-)

Charles

>>> Sean McInerney <seanm at nmr.mgh.harvard.edu> 09/30/04 09:31pm >>>
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...

_______________________________________________
This is the private VTK discussion list. 
Please keep messages on-topic. Check the FAQ at:
<http://public.kitware.com/cgi-bin/vtkfaq>
Follow this link to subscribe/unsubscribe:
http://www.vtk.org/mailman/listinfo/vtkusers




More information about the vtkusers mailing list