[Dcmlib] [Gdcm2] Assertion failed: ReferenceCount == 0
Mathieu Malaterre
mathieu.malaterre at gmail.com
Sun Aug 31 12:09:34 CEST 2008
Hi Greg,
On Sun, Aug 31, 2008 at 4:28 AM, Greg Book <gbook at gbook.org> wrote:
> I understand the appeal to not allow objects to be allocated on the stack.
> I'm most comfortable just using something like the following:
> Image *img = new Image();
> delete img;
Which is *exactly* identical to doing:
SmartPointer<Image> img = new Image;
except the syntax is a little less error prone IMHO.
> Will it be possible to use the "Image *img = reader.GetImage()" syntax in
> gdcm from now on?
You have a const reference right now, which is even better than a
pointer (no null checking required).
> I don't quite understand how to use the smartpointer in a loop though... I
> have a list of files, and I need to do some operation on all of them, so I
> would do something like the following:
>
> for (i=0;i<numfiles;i++) {
> /* open the dicom file */
> reader.SetFileName(filepath);
> if (!reader.Read()) {
> SetError(msg.Format("Dicomfile '%s' not readable -
> LoadFile->LoadDicomFilelist()",filepath.c_str()));
> return 0;
> }
> image = reader.GetImage();
>
> /* get the image data... */
> imageDataSize = image.GetBufferLength();
> tmpImageData = new unsigned char[imageDataSize];
> image.GetBuffer((char*)tmpImageData);
>
> ... etc ...
> }
Two things, unless you are using GDCM trunk (see recent bug fix in
copy/cstor of Object) you cannot do:
Image copy = reader.GetImage();
you are stuck with a :
const Image& ref = reader.GetImage();
Because a gdcm::ImageReader does not clean itself, you also have to
move the gdcm::ImageReader within the for loop:
which leaves you (gdcm 2.0.8) with something very ugly
std::vector < SmartPointer<Image>> images;
SmartPointer<Image> smartref;
for()
{
ImageReader r;
r.SetFileName( );
r.Read(); // check return
smartref = const_cast<gdcm::Image*>(&r.GetImage()); // very ugly
images.push_back( smartref );
}
I have implemented more operator in SmartPointer so the very ugly line
is now (in trunk):
smartref = r.GetImage();
But again as I said before copy cstor is fixed so previous code is
nice and clean:
std::vector < Image> images;
for()
{
ImageReader r;
r.SetFileName( );
r.Read(); // check return
images.push_back( r.GetImage() );
}
> How would I do the above with a smartpointer?
See above. SmartPointer are really just pointer, except they will do
something very smart in situation like:
SmartPointer<Image> ref;
{ // begin scope
ImageReader r;
r.SetFileName( );
r.Read()
ref = r.GetImage();
} // end scope
ref->Print( std::cout ); // this is valid !
You have 'transfered' ownership, nice and clean.
> -Greg
> ps - I keep posting to the dcmlib list because all my posts to the gdcm2
> list bounce back to me, even though I'm signed up for the list.
>
That's way beyond my understanding :(
Please contact the sf.net people, with a copy of the bounce email (cc
me when you have done the bug report) at:
https://sourceforge.net/projects/alexandria/support/
Thanks,
--
Mathieu
More information about the Dcmlib
mailing list