You should never have to call Register/UnRegister. The SmartPointers will take of that for you.<br><br>Here is the rule of thumb with respect to passing images into and out of routines:<br><br>"Methods/functions should take raw pointers are arguments and return raw pointers as return
<br>values."<br><br>This rule of thumb avoid unnecessary manipulations of the reference count in the object<br>and avoids the overhead of the mutex that surrounds the reference count.<br><br>THERE IS AN EXCEPTION TO THIS RULE:
<br><br>"If a method/function returns an image that it created AND no other reference to the<br>image is cached (for instance in an instance variable), then that image must returned<br>as a SmartPointer."<br><br>
This means the return type of a function should be a SmartPointer iff the function <br>created the image and did not store it in another persistent SmartPointer.<br><br>If the return type is not a SmartPointer in this case, the image is actually deallocated
<br>when you exit the routine.<br><br>So instead of return void*, try returning a IT_PTu16_3D::Pointer<br><br>Jim<br><br><br><br><div><span class="gmail_quote">On 4/17/06, <b class="gmail_sendername">Thomas Lambertz</b> <
<a href="mailto:thomas@hexerei-software.de">thomas@hexerei-software.de</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi there,<br><br>i have some newbie questions about the correct use of smart pointer in a<br>dll. Afaik itk uses smart pointer which have a livetime of the scope<br>where the are created in. So "all" i have to do is using itkImage (for
<br>example) within a common scope. Hm<br><br>Can i instantiate a itkImage in an class constructor ? (i would say no -<br>after return the image would be deleted - correct ?)<br>I first tried this way but i get randomly violations when accessing the
<br>itkimage. So i searched the mailinglistarchive and found this<br><a href="http://public.kitware.com/pipermail/insight-users/2004-November/011095.html">http://public.kitware.com/pipermail/insight-users/2004-November/011095.html
</a><br>mail from Luis Ibanez. He told that there would be two options for<br>approaching the problem. Option A was to create the image in a function<br>and pass the smart pointer to the processing functions. He recommend
<br>this way. As a result the processing functions have to be called within<br>this creating function (correct ?). But what about user interaction ?<br>For example read a slice series into a itkimage and then let the user
<br>scroll through the slices. I would prefer to have to functions/methods<br>to do this ReadSeriesIntoImage() and ShowSlice(). So when<br>ReadSeriesIntoImage was done i have to leave my dll and wait for some<br>user action - and if a have understand this correct i have no common
<br>scope between these two methods.<br>Option B was a kind of faking a bit with Register() then return and<br>then UnRegister(). I tried that way and i got violations by accessing<br>that image randomly (which makes me believe, that the memorystructure
<br>was overwritten by something else). Maybe someone has enough patience to<br>read my code...<br><br>Read the fileSeries: (seems to work properly - its a modified copy and<br>paste of what Luis wrote)<br>void * dDicom::myFunction( std::string* seriesID)
<br> {<br> FileNamesContainer fileNames;<br> nameGenerator->GetSeriesUIDs();<br> fileNames = nameGenerator->GetFileNames( seriesID->c_str() );<br><br> ReaderType_u16_3d::Pointer reader = ReaderType_u16_3d::New();
<br> reader->SetImageIO( dio );<br> reader->SetFileNames( fileNames );<br> try {<br> reader->Update();<br> }<br> catch (itk::ExceptionObject &ex) {<br> MessageBox(GetActiveWindow(),
ex.GetDescription() ,<br>"ReadSeries", MB_SETFOREGROUND);<br> }<br><br> IT_PTu16_3D::Pointer fakeimage = reader->GetOutput();<br> fakeimage->Register();<br> return fakeimage.GetPointer
();<br> }<br><br><br>a little wrapper<br>int dDicom::getFile(std::string* seriesID) {<br> storedImage=(IT_PTu16_3D *)this->myFunction(seriesID);<br> storedImage->UnRegister();<br><br> IT_PTu16_3D::RegionType inputRegion;
<br> inputRegion = storedImage->GetLargestPossibleRegion();<br> IT_PTu16_3D::SizeType size = inputRegion.GetSize();<br> slices = size[2];<br> return 1;<br>}<br><br><br>Showing a Slice:<br><br>void dDicom::showSlice(int slice) {
<br> try {<br> IT_PTu16_3D::SizeType size;<br> IT_PTu16_3D::RegionType inputRegion;<br><br>//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!<br>// next line is where the crashes sometimes(!) happens
<br>// structure of storedImage overwritten ?<br>// best way to provoke this on my maschine is to minimize/restore the<br>window and then call showSlice - but its not deterministic<br>// to prevent other sideeffects i excluded Paint and Resize Events from
<br>my Application<br> inputRegion = storedImage->GetLargestPossibleRegion();<br><br> size = inputRegion.GetSize();<br> int max = size[2];<br> size[2]=0; // only 2 dimensions<br><br> IT_PTu16_3D::IndexType start =
inputRegion.GetIndex();<br> start[2]=slice;<br><br> IT_PTu16_3D::RegionType desiredRegion;<br> desiredRegion.SetSize( size );<br> desiredRegion.SetIndex ( start );<br><br> filter->SetExtractionRegion( desiredRegion );
<br> filter->SetInput( storedImage );<br> filter->UpdateLargestPossibleRegion();<br><br> connector->SetInput( filter->GetOutput() );<br> connector->UpdateLargestPossibleRegion();
<br><br> myvtkviewer->SetInput( connector->GetOutput() );<br> myvtkviewer->SetSize(size[0],size[1]);<br> myvtkviewer->Render();<br> }<br> catch (itk::ExceptionObject &ex) {<br> MessageBox(GetActiveWindow(),
ex.GetDescription() , "ShowSlice",<br>MB_SETFOREGROUND);<br> }<br>}<br><br><br>Crashed could be localised in showSlice - i made some remarks before<br>that line. storedImage is a member variable of my class dDicom and
<br>should store the image after reading. Much code for a mail and i hope i<br>didnt bother you with that.<br><br>Ok - what can i do at this point ? Reading the sliceSeries everytime (<br>or even the requested slice) seems not very promising - espacially when
<br>refresh comes into play. One thought was to start a separate thread for<br>holding the image - but that sounds like breaking a butterfly on a wheel<br>to me.<br><br>Maybe someone would be kind enough to give me some hints for a solution.
<br><br>Thanks in advance,<br>with kind regards,<br>Tom<br>_______________________________________________<br>Insight-users mailing list<br><a href="mailto:Insight-users@itk.org">Insight-users@itk.org</a><br><a href="http://www.itk.org/mailman/listinfo/insight-users">
http://www.itk.org/mailman/listinfo/insight-users</a><br></blockquote></div><br>