[vtkusers] Render window

N Smethurst nick.smethurst at free.fr
Mon Mar 3 04:44:05 EST 2003


On Sun, Mar 02, 2003 at 06:46:16PM -0600, Tuhin Sinha wrote:
> I am a bit confused with the reference counting architecture in VTK.  My 
> understanding was that reference counting was used by an object to make
> sure it deleted itself when it wasn't needed anymore.  However, does this
> imply that the object _must_ wait until its reference count is 1 to delete
> itself? Or, can it delete itself regardless of who/how many things are
> pointed at it?

Tuhin,

I understand things as the following: At creation, an object's reference 
count is set to 1. It is necessary to call object->Delete() after assigning 
the object to something else, in order to take into account the reference 
count creation. For example, if you do the following:

vtkRenderer* ren = vtkRenderer::New();
vtkRendererWindow* renwin = vtkRendererWindow::New();
renwin->AddRenderer(ren);

then it is necessary to call ren->Delete() in order to reduce the reference 
count back to 1 (i.e. only one object is using it). Then, when renwin is 
destroyed, the reference count of ren is reduced by 1 and ren automatically 
destroys itself. If you don't do the initial ren->Delete(), this doesn't 
happen because ren still has it's creation reference.

There's nothing stopping you from repeatedly calling ren->Delete() until the 
object's reference count is zero whereupon the object destroys itself. 
However, you'll then be left with a load of objects that are pointing to 
deallocated memory and the resulting seg faults.

I noticed that there's also something a little strange about the reference 
counts and the relationship between a render window and an interactor, but 
I haven't explored that enough yet to comment fully.

Nick






More information about the vtkusers mailing list