[vtkusers] Does vtkInformation::Set(DATA_OBJECT, ...) perform a deep copy?
Dominique Töpfer
dominique at toepfer-web.de
Wed May 1 12:32:32 EDT 2013
On 01.05.2013 16:50, Berk Geveci wrote:
> If you want the output of an algorithm to stick around after deleting
> it, you need to call Register() on the data object. Something like:
>
> vtkSmartPointer<vtkReader1> Reader1 =
> vtkSmartPointer<vtkReader1>::New()
> // set filename and update reader1
> vtkDataSet * MyDataSet = Reader1->GetOutput();
> MyDataSet->Register(0);
>
>
Yes, but wouldn't you have to delete MyDataSet manually?
Why not using SmartPointers also for MyDataSet? From what I understand
after reading http://www.vtk.org/Wiki/VTK/Tutorials/SmartPointers , the
following code should also work and memory is released automatically
when the last pointer pointing to the data goes out of scope:
vtkSmartPointer<vtkDataSet> MyFilter::GetMyDataSet( ... )
{
vtkSmartPointer<vtkDataSet> MyDataSet;
if(SomeConditions)
{
vtkSmartPointer<vtkReader1> Reader1 =
vtkSmartPointer<vtkReader1>::New()
// set filename and update reader1
MyDataSet = Reader1->GetOutput();
}
else
{
vtkSmartPointer<vtkReader2> Reader2 =
vtkSmartPointer<vtkReader1>::New()
// set filename and update reader2
MyDataSet = Reader2->GetOutput();
}
return MyDataSet;
}
vtkSmartPointer<vtkDataSet> MyDataSet = GetMyDataSet( ... );
Or did I miss something?
Regards,
Dominique
> On Tue, Apr 30, 2013 at 5:14 PM, Sunrise <helios.corona at gmail.com
> <mailto:helios.corona at gmail.com>> wrote:
>
> Thanks for the answer, Berk.
>
> I have a follow up question, which I guess I can solve it with
> strong pointers. Assume the following
>
> vtkDataSet * MyFilter::GetMyDataSet(...)
> {
>
> if(SomeConditions)
> {
> vtkSmartPointer<vtkReader1> Reader1 =
> vtkSmartPointer<vtkReader1>::New()
> // set filename and update reader1
> vtkDataSet * MyDataSet = Reader1->GetOutput();
> }
> else
> {
> vtkSmartPointer<vtkReader2> Reader2 =
> vtkSmartPointer<vtkReader1>::New()
> // set filename and update reader2
> vtkDataSet * MyDataSet = Reader2->GetOutput();
> }
>
> // MyDataSet is out of scope here!
> // Even if I remove if condition, they will be out of scope
> outside of this function.
>
> return MyDataSet;
>
> }
>
> I want to make both reader1 and reader2 become SmartPointers.
> Otherwise I have leak. At compile time I do not know how the code
> will end, only at the run time it make the decision. So I do not
> know when to delete reader1 and reader2.
>
> I do not want to use descrutor too. there are more readers than
> the sample code that I put here, so at compile time the destructor
> do not know how many reader1 and/or reader2 has been declared in
> order to delete them manually. So, I prefer to declare all of them
> with Smart Pointers.
>
> But here is the problem: reader1 and reade2 are in clauses of If
> condition, and they are in a function outside of RequestData. So
> they will go out of scope.
>
> If using a strong pointer fix this problem, how can I apply a
> strong pointer on MyDataSet in VTK?
>
> Again, thank you very much.
>
> -Sia
>
>
>
> On 04/29/2013 08:20 AM, Berk Geveci wrote:
>> It does a shallow copy. The information object holds a strong
>> reference to the data object so
>>
>> outputInfo->Set(vtkDataObject::DATA_OBJECT(),MyDataSet);
>>
>> so it will increment the reference count of MyDataSet preventing
>> it from going out of scope.
>>
>> -berk
>>
>>
>> On Sun, Apr 28, 2013 at 11:41 AM, Sunrise
>> <helios.corona at gmail.com <mailto:helios.corona at gmail.com>> wrote:
>>
>> I am wondering if the "Set" method in vtkInformation class
>> performs a deep-copy or shallow-copy when I set a DATA_OBJECT
>> into it?
>>
>> Suppose I have some vtkDataSet and I want to append them into
>> outputInfo of a filter inside its RequestData method.
>>
>> int Filter1::RequestData(...)
>> {
>> vtkSmartPointer<vtkSomeReader> myreader =
>> vtkSmartPointer<vtkSomeReader>::New();
>> ...
>> vtkDataSet *MyDataSet = Reader->GetOutput();
>> outputInfo->Set(vtkDataObject::DATA_OBJECT(),MyDataSet);
>> ...
>> }
>>
>> at the end of RequestData, both the pointer myreader and its
>> data on the heap would be out of scope. Since I made them
>> smart pointers, both pointer "myreader" and the data on the
>> heap will be deleted (right?).
>>
>> But in the next filter of the pipeline (say Filter2 is
>> connected to Filter1), I can receive the data in inputInfo
>>
>> int Filter2::RequestData(...)
>> {
>> ...
>> ... inputInfo->Get(vtkDataObject::DATA_OBJECT());
>> }
>>
>> I am wondering why still I can get the data, since data
>> deferenced by MyDataSet in previous filter no longer exists?
>>
>> Does vtkInformation::Set perform a deep copy?
>>
>> Thank you.
>>
>> _______________________________________________
>> Powered by www.kitware.com <http://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
>>
>> Follow this link to subscribe/unsubscribe:
>> http://www.vtk.org/mailman/listinfo/vtkusers
>>
>>
>
>
> _______________________________________________
> Powered by www.kitware.com <http://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
>
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/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
>
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers
--
Dominique Töpfer, Dipl.-Inform.
Institute of Medical Physics
University of Erlangen
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20130501/d6a9a928/attachment.htm>
More information about the vtkusers
mailing list