[vtkusers] Question about Reference counting!!

Moreland, Kenneth kmorel at sandia.gov
Wed Aug 27 16:34:32 EDT 2008


Question 1: Why did the vtkImage reference count not decrease when the
reslicer was Deleted?  Answer: Because there is a circular reference
involving vtkImage and reslicer.  Thus, when you called Delete, no reference
went down to 0.  Don't worry, though.  There is some hidden garbage
collection happening behind the scenes so that once you delete both vtkImage
and reslicer, the lack of external references should be detected and both
should be deleted.  To verify that this is actually occurring (because you
apparently don't think that I'm trustworthy), you can turn on the
VTK_DEBUG_LEAKS CMake flag.  When on, there will be a warning any time a VTK
application exits without cleaning up all references.

Question 2: Why did the vtkImage reference count go from 1 to 3?  Answer:
Because the SetInput method is more complicated than you think.  It is not
as simple as simply calling Register and saving a pointer.  The method is
really just semantic sugar for setting up connections between ports on
vtkAlgorithm objects.  Since you created a vtkImageData by hand, it has no
vtkAlgorithm generating it.  To set up the pipeline connection correctly, a
dummy source for vtkImage is created.  It is this dummy vtkAlgorithm that is
creating the 3rd reference.

-Ken

On 8/27/08 12:00 AM, "namgyun Lee" <ggang56 at gmail.com> wrote:

> Hi, everyone!
>
> I am a new VTK user and trying to figure out how vtk reference counting is
> working.
> When I saw the result of simple code, I was kind of surprised.
>
> This is the sample code that I used to test reference counting.
>
> // test for reference counting
>     vtkImageData* vtkImage = vtkImageData::New();
>     std::cout << "Reference count: " << vtkImage->GetReferenceCount() <<
> std::endl;
>     std::cout << "Reference count: " << vtkImage->GetReferenceCount() <<
> std::endl;
>
>      vtkImageReslice* reslicer = vtkImageReslice::New();
>      reslicer->SetInput (vtkImage);
>      std::cout << "Reference count: " << vtkImage->GetReferenceCount() <<
> std::endl;
>      reslicer->Update();
>      std::cout << "Reference count: " << vtkImage->GetReferenceCount() <<
> std::endl;
>      reslicer->Delete();
>      std::cout << "Reference count: " << vtkImage->GetReferenceCount() <<
> std::endl;
>
>
> The output was 1 1 3 3 3!!!
> It seems like the reference count increases before the actual pipeline
> finishes updating.
>
> I always thought that if the container(reslicer) is deleted, the reference
> count of vtkImage  decreases as a result. And why did the reference become 3
> instead of 2?
> Am I thinking wrong? Could anyone explain about this?
>
> Thank you so much.
>
> Sincerely,
> Nam


   ****      Kenneth Moreland
    ***      Sandia National Laboratories
***********
*** *** ***  email: kmorel at sandia.gov
**  ***  **  phone: (505) 844-8919
    ***      fax:   (505) 845-0833





More information about the vtkusers mailing list