MagickCore memory problem

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Locked
sawadro

MagickCore memory problem

Post by sawadro »

I'm using MagickCore under VS 2005. I've tried to run MagickCore sample which is here http://www.imagemagick.org/script/magick-core.php

and figured out that at the beginning (this line) :

MagickCoreGenesis(*argv,MagickTrue);

this sample application takes 1 624 KB memory and at the end :

return(0);

it takes 3 276 KB memory.

Where is 1652 KB memory ? Why it doesn't free all allocated memory ?

Greetings
MS

User avatar
magick
Site Admin
Posts: 11254
Joined: 2003-05-31T11:32:55-07:00

Re: MagickCore memory problem

Post by magick »

We ran the sample program using ImageMagick 6.3.4-10 and checking for leaks with valgrind. There were 156 bytes still reachable but they were from the libtool library, not ImageMagick:
  • ==32397== ERROR SUMMARY: 508 errors from 20 contexts (suppressed: 0 from 0)
    ==32397== malloc/free: in use at exit: 156 bytes in 2 blocks.
    ==32397== malloc/free: 5,022 allocs, 5,020 frees, 4,886,593 bytes allocated.
    ==32397== For counts of detected errors, rerun with: -v
    ==32397== searching for pointers to 2 not-freed blocks.
    ==32397== checked 643,008 bytes.
    ==32397==
    ==32397==
    ==32397== 20 bytes in 1 blocks are still reachable in loss record 1 of 2
    ==32397== at 0x4004824: calloc (vg_replace_malloc.c:279)
    ==32397== by 0xCCF35B: _dlerror_run (in /lib/libdl-2.6.so)
    ==32397== by 0xCCEBA3: dlopen@@GLIBC_2.1 (in /lib/libdl-2.6.so)
    ==32397== by 0x4196D04: sys_dl_open (ltdl.c:1110)
    ==32397== by 0x419470F: tryall_dlopen (ltdl.c:2443)
    ==32397== by 0x4195E39: tryall_dlopen_module (ltdl.c:2510)
    ==32397== by 0x4196555: try_dlopen (ltdl.c:2542)
    ==32397== by 0x41969C2: lt_dlopen (ltdl.c:3437)
    ==32397== by 0x40F4E24: OpenModule (module.c:1076)
    ==32397== by 0x40F1198: GetMagickInfo (magick.c:456)
    ==32397== by 0x40F16C2: IsMagickConflict (magick.c:862)
    ==32397== by 0x415C667: GetPathComponent (utility.c:925)
    ==32397==
    ==32397==
    ==32397== 136 bytes in 1 blocks are still reachable in loss record 2 of 2
    ==32397== at 0x40054E5: malloc (vg_replace_malloc.c:149)
    ==32397== by 0xB3F80F: add_to_global (in /lib/ld-2.6.so)
    ==32397== by 0xB3FCC6: dl_open_worker (in /lib/ld-2.6.so)
    ==32397== by 0xB3BEC5: _dl_catch_error (in /lib/ld-2.6.so)
    ==32397== by 0xB3F4B1: _dl_open (in /lib/ld-2.6.so)
    ==32397== by 0xCCEC6C: dlopen_doit (in /lib/libdl-2.6.so)
    ==32397== by 0xB3BEC5: _dl_catch_error (in /lib/ld-2.6.so)
    ==32397== by 0xCCF2EB: _dlerror_run (in /lib/libdl-2.6.so)
    ==32397== by 0xCCEBA3: dlopen@@GLIBC_2.1 (in /lib/libdl-2.6.so)
    ==32397== by 0x4196D04: sys_dl_open (ltdl.c:1110)
    ==32397== by 0x419470F: tryall_dlopen (ltdl.c:2443)
    ==32397== by 0x4195E39: tryall_dlopen_module (ltdl.c:2510)
    ==32397==
    ==32397== LEAK SUMMARY:
    ==32397== definitely lost: 0 bytes in 0 blocks.
    ==32397== possibly lost: 0 bytes in 0 blocks.
    ==32397== still reachable: 156 bytes in 2 blocks.
    ==32397== suppressed: 0 bytes in 0 blocks.
Run a trace on leaked memory under Windows and post the trace here. If you can show the leak is within ImageMagick rather than a Windows system library, we will investigate further.

sawadro

Re: MagickCore memory problem

Post by sawadro »

OK. Lets take real working piece of code. I'm using MagickCore to convert images from 24bit to 8bit. I've got code :

MagickCoreGenesis(NULL,MagickTrue);
ExceptionInfo *exception;
Image *image,*images,*thumbnails;;
ImageInfo *image_info;
exception=AcquireExceptionInfo();
image_info=CloneImageInfo((ImageInfo *) NULL);
(void) strcpy(image_info->filename,filename);

images=ReadImage(image_info,exception);

thumbnails=NewImageList();
while ((image=RemoveFirstImageFromList(&images)) != (Image *) NULL)
{
QuantizeInfo *qinf=AcquireQuantizeInfo(image_info);
qinf->number_colors=256;
qinf->dither=MagickFalse;
QuantizeImage(qinf,image);
(void) AppendImageToList(&thumbnails,image);
}

(void) strcpy(thumbnails->filename,filename);
image_info->orientation=OrientationType::LeftTopOrientation;
WriteImage(image_info,thumbnails);
thumbnails=DestroyImageList(thumbnails);
image_info=DestroyImageInfo(image_info);
exception=DestroyExceptionInfo(exception);
MagickCoreTerminus();

and everything works perfectly. But I don't want read image from disk because I've got it rendered into memory and this (very similar) piece of code has memory problems :

MagickCoreGenesis(NULL,MagickTrue);
ExceptionInfo *exception;
Image *image,*images,*thumbnails;;
ImageInfo *image_info;
exception=AcquireExceptionInfo();
image_info=CloneImageInfo((ImageInfo *) NULL);
(void) strcpy(image_info->filename,filename);
//don't read from disk - get it from memory
images=ConstituteImage(width,height,"RGB",CharPixel,tbuf,exception);

thumbnails=NewImageList();
while ((image=RemoveFirstImageFromList(&images)) != (Image *) NULL)
{
QuantizeInfo *qinf=AcquireQuantizeInfo(image_info);
qinf->number_colors=256;
qinf->dither=MagickFalse;
QuantizeImage(qinf,image);
//need to flip image
image=FlipImage(image,exception);
//without setting this I can't save 8bit png
image->compression=CompressionType::ZipCompression;
image->depth=8;
(void) AppendImageToList(&thumbnails,image);
}

//from here - the same code
(void) strcpy(thumbnails->filename,filename);
image_info->orientation=OrientationType::LeftTopOrientation;
WriteImage(image_info,thumbnails);
thumbnails=DestroyImageList(thumbnails);
image_info=DestroyImageInfo(image_info);
exception=DestroyExceptionInfo(exception);
MagickCoreTerminus();

where tbuf is declared :

unsigned char * tbuf=new unsigned char[width*height*3];
width=250
height=250

This code works almost perfectly - every time I use it, it takes and doesn't free approx 700KB memory.

sawadro

Re: MagickCore memory problem

Post by sawadro »

I convert to .png images (from .png if I read it from disk).

el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: MagickCore memory problem

Post by el_supremo »

I think the problem is that in the second version you have images=ConstituteImage(...) but you don't destroy "images" later.

Pete

Locked