[Insight-users] returning a smart pointer from a function
Jim Miller
millerjv at gmail.com
Sun Feb 6 12:59:30 EST 2011
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