[Insight-users] passing itk::BSplineDeformableTransform between
functions
Luis Ibanez
luis.ibanez at kitware.com
Thu Aug 3 13:02:09 EDT 2006
Hi Ziv,
I agree with your purist view.
This is actually a good suggestion that we could easily implement.
We will have to figure out how to sort out the backward compatibility
issues.
Could you please log a bug entry regarding this issue ?
http://public.kitware.com/Bug/index.php
In this way we will make sure to fix this before the next release.
Thanks
Luis
------------------
Ziv Yaniv wrote:
> Hi Luis,
>
> This solution is what I was using but I thought that I was missing
> something with the use of this class. In any case this is not a generic
> solution as you assume that the calling function releases the
> parameters. If the transformation is passed around (twice or more down
> the call stack) you'll have the memory leak. The "elegant"/more generic
> solution is to add a GetParameters() method that returns a pointer to
> the data so that the user can release the memory at his/her own
> discretion. While this method will break with the ITK practice of
> encapsulation the code is already "broken" as the class points to an
> external data structure.
>
> From a purist point of view I would add a method
> SetParameters(ParametersType ¶m, bool copyInput). This way the user
> can specify if the BSplineDeformableTransform should make an internal
> copy or just point to the data. Additionally, the class will have to
> keep this flag internally so that when an object is destroyed it either
> frees the memory or not. I used this approach in an implementation of a
> kD-tree, and it worked for me. This way I got the "best" of both worlds.
> If I knew I could release the data in the calling method then I set
> copyInput=false and the operation was faster. If on the other hand I
> needed to pass the kD-tree around then I copied the data and the
> object's destructor took care of releasing it.
>
> sorry for the rant
> Ziv
>
>
> Luis Ibanez wrote:
>
>>
>> Hi Ziv,
>>
>> How about creating the array before calling the function,
>> and then destroying it after the function returns ?
>>
>>
>> something like:
>>
>>
>> void f()
>> {
>>
>> itk::BSplineDeformableTransform<>::ParametersType *param = new
>> itk::BSplineDeformableTransform<>::ParametersType();
>>
>> itk::Transform<>::Pointer transformation = itk::Transform<>;
>> FixedImageType::Pointer fixedImage = FixedImageType::New();
>> MovingImageType::Pointer movingImage = MovingImageType::New();
>>
>> transformation->SetParameters( *param );
>>
>> register(fixedImage, movingImage, transformation);
>>
>> // Use transformation...
>>
>> // delete the parameters array.
>> delete param;
>>
>> }
>>
>>
>>
>> It is not particularly pretty, but it should release
>> the memory correctly.
>>
>>
>> Please let us know if that works for you.
>>
>>
>>
>> Thanks
>>
>>
>>
>> Luis
>>
>>
>>
>> -----------------
>> Ziv Yaniv wrote:
>>
>>> Hi all,
>>>
>>> I'm using the itk::BSplineDeformableTransform class and I was
>>> wondering how I pass an object of this
>>> type between functions without causing a memory leak. The problem is
>>> that the transformation object only holds a
>>> pointer to the parameters array and does not give access to the
>>> pointer later on so that the user can release the
>>> memory.
>>>
>>> This is my scenario in pseudocode:
>>>
>>> void f() {
>>>
>>> itk::Transform<>::Pointer transformation = itk::Transform<>;
>>> FixedImageType::Pointer fixedImage = FixedImageType::New();
>>> MovingImageType::Pointer movingImage = MovingImageType::New();
>>>
>>> register(fixedImage, movingImage, transformation);
>>>
>>> //At this location I have the transformation and I can
>>> downcast it to
>>> //BSplineDeformableTransform, but I can't get the pointer
>>> to the parameters array to release it
>>> }
>>>
>>> void register(FixedImageType::Pointer & fixedImage,
>>> MovingImageType::Pointer & movingImage,
>>> itk::Transform<>::Pointer &transformation)
>>> {
>>> itk::BSplineDeformableTransform<>::Pointer bSplineTransformation =
>>> itk::BSplineDeformableTransform<>::New();
>>>
>>> transformation = bSplineTransformation; //point to what will be
>>> the result
>>>
>>> //allocate the parameters array on the heap so that it
>>> still exists after we exit the method
>>> itk::BSplineDeformableTransform<>::ParametersType *param = new
>>> itk::BSplineDeformableTransform<>::ParametersType();
>>>
>>> bSplineTransformation->SetParameters(*param);
>>>
>>> }
>>>
>>> Perhaps I'm missing something.
>>>
>>> thanks
>>> Ziv
>>>
>>> _______________________________________________
>>> Insight-users mailing list
>>> Insight-users at itk.org
>>> http://www.itk.org/mailman/listinfo/insight-users
>>>
>>>
>>
>>
>>
>>
>
>
More information about the Insight-users
mailing list