[Insight-users] returning a smart pointer from a function

Dan Mueller dan.muel at gmail.com
Mon Feb 7 01:18:38 EST 2011


Hi Ricardo,

Your original code was just fine. Because the reader will be destroyed
when the function exits, there is no need to disconnect the pipeline.

Sorry for the confusion.

Cheers, Dan

On 6 February 2011 23:20, Ricardo Ferrari
<ricardo.jose.ferrari at gmail.com> wrote:
> Hi Guys,
>
> Thank you very much for your quickly answers. So, as I understood, the
> correct way for my piece of code would be
>
> ///
> ****************************************************************************************************
> ///
> ///
> ****************************************************************************************************
> template< typename TImageType >
> typename TImageType::Pointer ReadImage( const std::string fileName )
> {
>    typedef itk::ImageFileReader< TImageType >    ImageFileReader;
>    typename ImageFileReader::Pointer reader = ImageFileReader::New();
>    reader->SetFileName( fileName );
>
>    try
>    {
>        reader->Update();
>    }
>    catch (... )
>    {
>        std::cout << "Error while reading an image" << fileName << std::endl;
>        throw;
>    }
>
>    TImageType::Pointer out = reader->GetOutput();
>    out->DisconnectPipeline();
>
>    return out; //reader->GetOutput();
> }
>
>
>
>
> On 02/06/2011 06:34 PM, Dan Mueller wrote:
>>
>> Thank Jim,
>>
>> Seems I wrote my email too quickly, and didn't give enough thought.
>> You are correct on all accounts. Thanks for clarifying my remarks.
>>
>> Cheers, Dan
>>
>> On 6 February 2011 18:59, Jim Miller<millerjv at gmail.com>  wrote:
>>>
>>> Because the reader is going out of scope, you shouldn't need the
>>> DisconnectPipeline() call. I will sometimes use one is such circumstances to
>>> be clear that I want the filters to go away.
>>>
>>> Note that to do this properly, you would need to assign the output to a
>>> local variable (smart pointer), then call DisconnectPipeline on the local
>>> variable, and finally return the local variable (and not output of the
>>> reader).
>>>
>>> If you were to call DisconnectPipeline on the output of the reader
>>> directly the data you just read would be lost and a new empty output would
>>> be created.
>>>
>>> Jim
>>>
>>>
>>>
>>> On Feb 6, 2011, at 11:33 AM, Dan Mueller<dan.muel at gmail.com>  wrote:
>>>
>>>> Hi Ricardo,
>>>>
>>>> I would also add a call to reader->GetOutput()->DisconnectPipeline()
>>>> (after the reader->Update() call) to make sure the image is
>>>> disconnected from the pipeline... Other than that, this function seem
>>>> fine.
>>>>
>>>> HTH
>>>>
>>>> Cheers, Dan
>>>>
>>>> On 6 February 2011 17:27, Ricardo Ferrari<rjf.araraquara at gmail.com>
>>>>  wrote:
>>>>>
>>>>> Hi guys,
>>>>>
>>>>> I am just wondering if is there any disadvantage in using the following
>>>>> function to encapsulate the image file reading.
>>>>>
>>>>> Thank you,
>>>>> Ricardo
>>>>>
>>>>>
>>>>> ///
>>>>>
>>>>> ************************************************************************************************************
>>>>> ///
>>>>> ///
>>>>>
>>>>> ************************************************************************************************************
>>>>> template<  typename TImageType>
>>>>> typename TImageType::Pointer ReadImage( const std::string fileName )
>>>>> {
>>>>>     typedef itk::ImageFileReader<  TImageType>      ImageFileReader;
>>>>>     typename ImageFileReader::Pointer reader = ImageFileReader::New();
>>>>>     reader->SetFileName( fileName );
>>>>>
>>>>>     try
>>>>>     {
>>>>>         reader->Update();
>>>>>     }
>>>>>     catch (... )
>>>>>     {
>>>>>         std::cout<<  "Error while reading in image"<<  fileName<<
>>>>> std::endl;
>>>>>         throw;
>>>>>     }
>>>>>
>>>>>     return reader->GetOutput();
>>>>> }
>>>>>
>>>>>
>>>>> The function is used as follows:
>>>>>
>>>>> InputImageType::Pointer img = ReadImage<  InputImageType>(
>>>>> inputFileName );
>>>>
>>>> _____________________________________
>>>> Powered by www.kitware.com
>>>>
>>>> Visit other Kitware open-source projects at
>>>> http://www.kitware.com/opensource/opensource.html
>>>>
>>>> Kitware offers ITK Training Courses, for more information visit:
>>>> http://www.kitware.com/products/protraining.html
>>>>
>>>> Please keep messages on-topic and check the ITK FAQ at:
>>>> http://www.itk.org/Wiki/ITK_FAQ
>>>>
>>>> Follow this link to subscribe/unsubscribe:
>>>> http://www.itk.org/mailman/listinfo/insight-users
>
>


More information about the Insight-users mailing list