[Insight-users] Error using SetImportPointer
richyvr
richyvr at gmail.com
Wed Jan 19 06:13:34 EST 2011
This is the exact problem i am talking about. for me both of the options
doesnt seem to be working. it gives me a run time error.
"Hi Vijay,
You must *always* assign the result of a New() operator
to a SmartPointer. Like in:
typedef itk::Image<unsigned char, 2> ImageType;
ImageType::Pointer img = ImageType::New();
When creating an ITK image inside a function you have the
following two options from preventing the image from
being destroyed at the time the function returns:
Option A:
Create the SmartPointer of the image type outside
the function and pass it as a pointer. Then inside
the function assign the image to that passed Smart
Pointer.
If you are doing this in a C-like interface, that will
look like
// before calling the function
typedef itk::Image<unsigned char, 2> ImageType;
ImageType::Pointer img;
void * pimg = &img;
myFunctionForCreatingAnImage( pimg, k1, k2, k3,... kn );
// inside the function
void myFunctionForCreatingAnImage( void * pimg, k1, k2 ... )
{
typedef itk::Image<unsigned char, 2> ImageType;
// your code for the importer filter..
ImageType::Pointer *kimg = (ImageType::Pointer *)pimg;
*kimg = importer->GetOutput();
return;
}
Option B:
You can artificially increase the reference count
of the image before returning from the function,
and return the raw pointer to the image in the form
of a (void *).
// Inside the function
void * myFunction( k1, k2,...)
{
typedef itk::Image<unsigned char, 2> ImageType;
// do the import stuff...
importer->Update(); // IMPORTANT !!!
ImageType::Pointer image = importer->GetOutput();
image->Register(); // Increment the reference count
return image.GetPointer();
}
// Outside the function
ImageType::Pointer image = (ImageType *)myFunction(...);
image->UnRegister(); // Decrement the reference count
// start happily using the image....
Option "A" is by far a better approach...
Please let us know if you have further questions,
Thanks
Luis
"
option 1 runs with out issues if you create importFilter->SetImportPointer(
pImageBuffer, nPixels, 0);
but when i call it using importFilter->SetImportPointer( pImageBuffer,
nPixels, 1); it crashes in run time pointing it to some corrupted pointer
location.
Kindly help ASAP.
Regards,
Richard
--
View this message in context: http://itk-insight-users.2283740.n2.nabble.com/Error-using-SetImportPointer-tp5938823p5939178.html
Sent from the ITK Insight Users mailing list archive at Nabble.com.
More information about the Insight-users
mailing list