<div dir="ltr"><br><br><div class="gmail_quote">On Sun, Aug 31, 2008 at 10:56 PM, namgyun Lee <span dir="ltr"><<a href="mailto:ggang56@gmail.com">ggang56@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div dir="ltr">I have a further question about this.<br><br>Let me describe the situation briefly.<br><br>I have defined all types in the header file of class and also defined a private member variable "list" as std::vector<ImageType::Pointer>.<br>
And I return the ImageType::Pointer to the "list" using two different methods,<br><br>1) list.push_back(this->readImg("brain.dcm"));<br>2)ImageType::Pointer myImage = this->readImg("brain.dcm");<br>
list.push_back(myImage);<br><br>My question is what is the difference between the methods describe above?<br>Could you explain the difference emphasizing the scope and reference counting perspective?<br><br><br>Thank you so much.<br>
<br>Sincerely,<br><font color="#888888">Namgyun Lee</font><div><div></div><div class="Wj3C7c"><br><br><br><br><div class="gmail_quote">On Sun, Aug 31, 2008 at 7:56 AM, Luis Ibanez <span dir="ltr"><<a href="mailto:luis.ibanez@kitware.com" target="_blank">luis.ibanez@kitware.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><br>
Hi Protein,<br>
<br>
<br>
In order to keep an ITK image alive when returning it from<br>
a function, you *MUST* return it as a SmartPointer.<br>
<br>
Instead of your current signature:<br>
<br>
ImageType* readImg(char* imageFileName);<br>
<br>
You should use<br>
<br>
ImageType::Pointer readImg(char* imageFileName);<br>
<br>
<br>
In that way the smart pointer will be copied in the stack when<br>
the function is returning, and that temporary copy wil keep the<br>
image alive long enough to survive the destruction of data in<br>
the local scope of the function.<br>
<br>
Make sure that the returned variable is assigned to a<br>
SmartPointer as well.<br>
<br>
<br>
That is, when you call the function do something like:<br>
<br>
ImageType::Pointer myImage = readImg("brain.dcm");<br>
<br>
<br>
NOTE: You *do not* need, and you *should not* modify the<br>
reference counting of ITK images. SmartPointers<br>
are supposed to do that for you.<br>
<br>
<br>
<br>
Regards,<br>
<br>
<br>
Luis<br>
<br>
<br>
---------------<div><div></div><div><br>
protein wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hey all,<br>
<br>
This might be a duplicated question but I really didn't find a answer..<br>
<br>
Basically my scenario could be described as to write a native function<br>
to read a .png file, using an instantiated itk::ImageFileReader<unsigned char>,<br>
and return an itk::Image<unsigned char>. How to keep that itk::Image alive<br>
after returning from the native function?<br>
<br>
So in the header file I defined:<br>
<br>
const unsigned int Dimension = 2;<br>
typedef float PixelType;<br>
typedef itk::Image< PixelType, Dimension > ImageType;<br>
typedef itk::ImageFileReader< ImageType > ImageReaderType;<br>
<br>
then I declear a function, which takes<br>
char* imageFileName as input<br>
and<br>
output an ImageType*<br>
<br>
ImageType* readImg(char* imageFileName);<br>
<br>
the definition readImg function:<br>
ImageReaderType reader = ImageReaderType::New();<br>
reader->SetFileName(imageFileName);<br>
reader->Update();<br>
return reader->GetOutput();<br>
<br>
<br>
But when calling this function in main(), by:<br>
ImageType::Pointer img = readImg("test.png");<br>
<br>
I know the reader is destructed when going back to main, thus the<br>
there will be error.<br>
<br>
So I increased the reference count of reader before returning,<br>
to keep it alive even after returned from the readImg function, I added:<br>
reader->SetReferenceCount(1+reader->GetReferenceCount());<br>
before "return reader->GetOutput();"<br>
<br>
Then in the main, I could use the img.<br>
<br>
However this seems not that nice to me since reader won't get released....<br>
<br>
<br>
Could anyone tell me in general how to deal with such kind of problems?<br>
I really appreciate that!<br>
<br>
Thanks,<br>
Yi<br>
_______________________________________________<br>
Insight-users mailing list<br>
<a href="mailto:Insight-users@itk.org" target="_blank">Insight-users@itk.org</a><br>
<a href="http://www.itk.org/mailman/listinfo/insight-users" target="_blank">http://www.itk.org/mailman/listinfo/insight-users</a><br>
<br>
</blockquote>
_______________________________________________<br>
Insight-users mailing list<br>
<a href="mailto:Insight-users@itk.org" target="_blank">Insight-users@itk.org</a><br>
<a href="http://www.itk.org/mailman/listinfo/insight-users" target="_blank">http://www.itk.org/mailman/listinfo/insight-users</a><br>
</div></div></blockquote></div><br></div></div></div>
</blockquote></div><br></div>