[Insight-users] Using smart pointer in a dll

Thomas Lambertz thomas at hexerei-software.de
Tue Apr 18 07:42:29 EDT 2006


Thanks to your fast replay,

i tried it this way:

in dDicom-class:

...
private:
   IT_PTu16_3D::Pointer storedImage;
...


Methods:

IT_PTu16_3D::Pointer loadImage( std::string* seriesID) {
   ...
   ReaderType_u16_3d::Pointer reader = ReaderType_u16_3d::New();
   ...
   // return smart pointer
   return reader->GetOutput();
}


int dDicom::getFile(std::string* seriesID) {
   storedImage = loadImage( seriesID);
   // at this time all seemes to be right
   ...
}


void dDicom::showSlice(int slice) {
   // check if storedImage still exists
   if (storedImage.IsNull()) Messagebox(...);
      // randomly it doesnt
}



So the Image is not on the heap but on the stack - right ? What can i do 
to prevent this ? What am i missing ?

regards,
Tom





Jim Miller schrieb:
 > You should never have to call Register/UnRegister.  The SmartPointers 
will take of that for you.
 >
 > Here is the rule of thumb with respect to passing images into and out 
of routines:
 >
 > "Methods/functions should take raw pointers are arguments and return 
raw pointers as return
 > values."
 >
 > This rule of thumb avoid unnecessary manipulations of the reference 
count in the object
 > and avoids the overhead of the mutex that surrounds the reference count.
 >
 > THERE IS AN EXCEPTION TO THIS RULE:
 >
 > "If a method/function returns an image that it created AND no other 
reference to the
 > image is cached (for instance in an instance variable), then that 
image must returned
 > as a SmartPointer."
 >
 > This means the return type of a function should be a SmartPointer iff 
the function
 > created the image and did not store it in another persistent 
SmartPointer.
 >
 > If the return type is not a SmartPointer in this case, the image is 
actually deallocated
 > when you exit the routine.
 >
 > So instead of return void*, try returning a IT_PTu16_3D::Pointer
 >
 > Jim
 >





More information about the Insight-users mailing list