[vtkusers] Render window

Andrew J. P. Maclean a.maclean at cas.edu.au
Mon Mar 3 19:25:30 EST 2003


My understanding of the process is as follows:
The object cannot delete itself if references exist to it, i.e. if its
reference count > 1.
Look at: void vtkObjectBase::UnRegister(vtkObjectBase* o) to see how it
works. You also need to remember that if object A is referenced by
object B then somewhere in B->Delete(), A->Delete() will be called.

Nik's method (below) will only work if you DO NOT destroy ren on exit.
In other words, you do something like this:
vtkRenderer* ren = vtkRenderer::New();
vtkRendererWindow* renwin = vtkRendererWindow::New();
renwin->AddRenderer(ren); // ren's reference count is now 2
ren->Delete(); ////////


.... render etc. ...

renwin->Delete(); // will also delete ren because its reference count is
1 [renwin will try a Delete on ren] 


However the most common way is this:
vtkRenderer* ren = vtkRenderer::New();
vtkRendererWindow* renwin = vtkRendererWindow::New();
renwin->AddRenderer(ren); // ren's ref count is 2

.... render etc. ...

ren->Delete(); // ren's ref count is now 1
renwin->Delete(); // will also delete ren because its reference count is
1 [renwin will try a Delete on ren]

If we swap around the deletes we have:
renwin->Delete(); // renwin will try a Delete on ren and it's reference
count will drop from 2 to 1.
ren->Delete(); // ren will be deleted because it's reference count is 1

I hope this is useful.

Andrew


___________________________________________

Andrew J. P. Maclean

Centre for Autonomous Systems
The Rose Street Building J04
The University of Sydney  2006  NSW
AUSTRALIA

Ph: +61 2 9351 3283
Fax: +61 2 9351 7474

URL: http://www.cas.edu.au/
___________________________________________


-----Original Message-----
From: vtkusers-admin at public.kitware.com
[mailto:vtkusers-admin at public.kitware.com] On Behalf Of N Smethurst
Sent: Monday, 3 March 2003 20:44
To: vtkusers at public.kitware.com
Subject: Re: [vtkusers] Render window

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