[vtkusers] Reference counting in 6.3
Weiguang guan
guanw at rhpcs.mcmaster.ca
Mon Oct 26 13:13:46 EDT 2015
Thanks for the insight, David. I'm less worried from now on when seeing
reference counting doesn't work the way I thought it should.
Best,
Weiguang
On 26/10/2015 11:36 AM, David Cole wrote:
> It depends on what A and B are, and what happens when you connect them ...
>
> With this line of code:
>
> A->SetInputConnection(B->GetOutputPort());
>
> I would expect that the object returned by B's GetOutputPort method is
> the one whose ref count is increased. (But it also may not be,
> depending on what connections already exist prior to the call...)
>
> Neither, or one only, or both ref counts may go up depending on
> whether they are already ref-counted by other objects in the pipeline.
> In general, you cannot depend on the ref-counts changing when you
> connect or disconnect objects.
>
> You can count on this: if something needs to be kept alive by
> something else, it will either already have an existing ref count on
> it, or increase the ref count to keep it alive.
>
> If something is being detached or disconnected, it may or may not see
> its ref count decremented. It's totally context dependent: you have to
> dig in and learn about all the objects involved and understand all
> their interconnections to figure out what's going on.
>
>
> Hope this helps,
> David C.
>
>
>
> On Mon, Oct 26, 2015 at 10:31 AM, Weiguang guan <guanw at rhpcs.mcmaster.ca> wrote:
>> Hi David,
>>
>> Thank you very much for helping me understand reference counting in VTK.
>> Most of my questions are answered except this one:
>>
>> A->SetInputConnection(B->GetOutputPort());
>>
>> Why A's reference count is incremented after the above statement, not B's? I
>> think B is used by A, so that B's reference count should be incremented. Am
>> I right?
>>
>> Best,
>> Weiguang
>>
>> On 23/10/2015 5:07 PM, David Cole wrote:
>>> There is nothing that guarantees the reference count of an object
>>> derived from vtkObject will be any certain value immediately after
>>> construction.
>>>
>>> It totally depends on what happens to the object during its
>>> constructor. It might set up a whole network of other objects, and
>>> then give references to itself to some of them. Some of them might
>>> increase the reference count of the object, some of them might not.
>>>
>>> If you call Register, the reference count goes up. If you call
>>> Unregister, it goes down. If it goes down far enough, the object's
>>> destructor is called and the memory is released.
>>>
>>> The mechanism itself has not changed significantly lately (that I know
>>> of...), but individual objects may have changed their behavior with
>>> respect to "what's the reference count immediately after construction"
>>> or " what's the count after a single or repeated calls to
>>> SetInputConnection..."
>>>
>>> You should not rely on the reference count value for anything other
>>> than automatic cleanup when no more references exist.
>>>
>>>
>>> HTH,
>>> David C.
>>>
>>>
>>>
>>> On Fri, Oct 23, 2015 at 4:24 PM, Weiguang guan <guanw at rhpcs.mcmaster.ca>
>>> wrote:
>>>> Hi Dan,
>>>>
>>>> Thank you very much for the reply.
>>>>
>>>> I still use the old way (vtkABC *v= vtkABC::New()) rather than using VTK
>>>> smart pointer. The reference counting mechanism of VTK must have changed
>>>> although I don't know when (which version) that happened.
>>>>
>>>> The reference count of a VTK object used to be 1 right after its
>>>> creation.
>>>> But now it is not always the case. I find source objects (like
>>>> vtkCubeSource, vtkImageEllipsoidSource, etc) have reference count = 2
>>>> right
>>>> after being created while filter object have reference count = 1. I
>>>> haven't
>>>> checked every class to see if there is an exception to the above finding.
>>>>
>>>> Here is a three-line code section together with running results:
>>>> vtkCubeSource* cube = vtkCubeSource::New(); //
>>>> ReferenceCount of cube = 2
>>>> vtkShrinkPolyData* shrink = vtkShrinkPolyData::New(); //
>>>> ReferenceCount
>>>> of cube = 2, ReferenceCount of shrink= 1
>>>> shrink->SetInputConnection(cube->GetOutputPort()); //
>>>> ReferenceCount
>>>> of cube = 2, ReferenceCount of shrink= 2
>>>> It can not be explained by the old VTK reference counting mechanism.
>>>>
>>>> Best,
>>>> Weiguang
>>>>
>>>> On 21/10/2015 3:11 PM, Dan Lipsa wrote:
>>>>
>>>> Hi Weiguang,
>>>> Take a look at
>>>> http://www.vtk.org/Wiki/VTK/Tutorials/SmartPointers
>>>> This might clarify some of your questions. You have to remember that the
>>>> reference count is stored in the objects themselves unlike the c++
>>>> shared_ptr where the reference count is stored in the shared pointer
>>>> object.
>>>>
>>>> For the first question: do you assign the object to a vtk smart pointer?
>>>> That would explain why the reference count is 2.
>>>> For the rest of the questions: when you set up the pipeline there are
>>>> other
>>>> objects involved besides the filter themselves, so it is harder to
>>>> understand of the reference count.
>>>>
>>>> Also you can set VTK_DEBUG_LEAKS when you build VTK if you are concerned
>>>> about leaks.
>>>>
>>>> Dan
>>>>
>>>>
>>>> On Thu, Oct 15, 2015 at 4:13 PM, Weiguang guan <guanw at rhpcs.mcmaster.ca>
>>>> wrote:
>>>>> Hello,
>>>>>
>>>>> I find, in debug mode, that the ReferenceCount of a
>>>>> vtkImageEllipsoidSource object becomes 2 right after being initiated
>>>>> (vtkImageEllipsoidSourc::New()). It should be 1 not 2, right? How could
>>>>> this
>>>>> happen?
>>>>>
>>>>> My second question is --- when you make a connection
>>>>> A->SetInputConnection(B->GetOutputPort()); Whose reference count will be
>>>>> increased by 1, A or B? I think B's reference count should be increased
>>>>> because it is used by A. But what I find is just the opposite.
>>>>>
>>>>> My third question is about setting up more than 1 connection for a
>>>>> filter.
>>>>> Taking vtkImageMathematics as an example, I do
>>>>> vtkImageMathematics* A = vtkImageMathematics::New();
>>>>> A->SetInputConnection(0, B1->GetOutputPort();
>>>>> A->SetInputConnection(0, B2->GetOutputPort();
>>>>>
>>>>> I find that the reference count of A gets increased by the first
>>>>> SetInputConnection() call only.
>>>>>
>>>>> Can someone explain how reference counting works in VTK? Thanks.
>>>>>
>>>>> Best,
>>>>> Weiguang
>>>>> _______________________________________________
>>>>> Powered by www.kitware.com
>>>>>
>>>>> Visit other Kitware open-source projects at
>>>>> http://www.kitware.com/opensource/opensource.html
>>>>>
>>>>> Please keep messages on-topic and check the VTK FAQ at:
>>>>> http://www.vtk.org/Wiki/VTK_FAQ
>>>>>
>>>>> Search the list archives at: http://markmail.org/search/?q=vtkusers
>>>>>
>>>>> Follow this link to subscribe/unsubscribe:
>>>>> http://public.kitware.com/mailman/listinfo/vtkusers
>>>>
>>>>
>>>>
>>>> _______________________________________________
>>>> Powered by www.kitware.com
>>>>
>>>> Visit other Kitware open-source projects at
>>>> http://www.kitware.com/opensource/opensource.html
>>>>
>>>> Please keep messages on-topic and check the VTK FAQ at:
>>>> http://www.vtk.org/Wiki/VTK_FAQ
>>>>
>>>> Search the list archives at: http://markmail.org/search/?q=vtkusers
>>>>
>>>> Follow this link to subscribe/unsubscribe:
>>>> http://public.kitware.com/mailman/listinfo/vtkusers
>>>>
More information about the vtkusers
mailing list