Page 1 of 1

No memory released after reading one image

Posted: 2009-10-10T14:57:57-07:00
by Wilbert
I'm using 6.5.6-9 and the MagickCore api. It seems that the memory is not being released after reading one image. I used the following code:

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <magick/MagickCore.h>
#include <windows.h>

void main(int argc, char *argv[])
	
{
	ExceptionInfo *exception;
	Image *image, *images, *resize_image;
    ImageInfo *image_info;

	char BUF[256];
	int j;
    for (j=1; j<65; j++) {
		sprintf(BUF, "001-%03d.jpg", j);
		OutputDebugString(BUF);

		MagickCoreGenesis(argv[1], MagickTrue);
		image_info = CloneImageInfo((ImageInfo *) NULL);
		exception = AcquireExceptionInfo();

		(void) strcpy(image_info->filename, BUF);

		images = ReadImage(image_info, exception);
		images = CoalesceImages(images, exception);
		image = GetImageFromList(images, 0);

		resize_image = ResizeImage(image,640,480,LanczosFilter,1.0,exception);
  
	    // Write the image thumbnail
		(void) strcpy(resize_image->filename, "thumbnail.JPG");
	    WriteImage(image_info, resize_image);

		DestroyImage(image);
	    DestroyImage(resize_image);
		DestroyImageInfo(image_info);
	    DestroyExceptionInfo(exception);
		MagickCoreTerminus();
	}
}
When running the executable the memory usage keeps growing and growing until all the images (size: ~3000x2000) are processed. After processing 50 images the mem usage is already at 1.750.000. It drops back to 486.000 when the program is ready. It this a bug, or should i change the code?

Re: No memory released after reading one image

Posted: 2009-10-10T15:18:09-07:00
by magick
You have a leak here:
  • images = ReadImage(image_info, exception);
    images = CoalesceImages(images, exception);
You also need to take MagickCoreGenesis() and MagickCoreTerminus() methods outside the loop.

Re: No memory released after reading one image

Posted: 2009-10-10T15:28:56-07:00
by Wilbert
Thanks for the fast response!
You also need to take MagickCoreGenesis() and MagickCoreTerminus() methods outside the loop.
Ok.
You have a leak here:

images = ReadImage(image_info, exception);
images = CoalesceImages(images, exception);
So, how should i change my code? If i change it as follows:

Code: Select all

Image *image, *images, *images2, *resize_image;
// ...
images = ReadImage(image_info, exception);
images2 = CoalesceImages(images, exception);
the same problem remains.

Re: No memory released after reading one image

Posted: 2009-10-10T15:31:06-07:00
by magick
Every image created must be destroyed. Use DestroyImageList(images).

Re: No memory released after reading one image

Posted: 2009-10-10T15:38:11-07:00
by Wilbert
If i do that, my program crashes:

Code: Select all

//..
   	MagickCoreGenesis(argv[1], MagickTrue);
	for (j=1; j<65; j++) {
		sprintf(BUF, "001-%03d.jpg", j);
		OutputDebugString(BUF);

		image_info = CloneImageInfo((ImageInfo *) NULL);
		exception = AcquireExceptionInfo();

		(void) strcpy(image_info->filename, BUF);

		images = ReadImage(image_info, exception);
		images2 = CoalesceImages(images, exception);
		image = GetImageFromList(images2, 0);

		resize_image = ResizeImage(image,640,480,LanczosFilter,1.0,exception);
  
	    // Write the image thumbnail
		(void) strcpy(resize_image->filename, "thumbnail.JPG");
	    WriteImage(image_info, resize_image);

		DestroyImage(image);
	    DestroyImage(resize_image);
		DestroyImageList(images);
		DestroyImageList(images2);
		DestroyImageInfo(image_info);
	    DestroyExceptionInfo(exception);
	}

	MagickCoreTerminus();
Something about the 'memory couldn't be read'.

Re: No memory released after reading one image

Posted: 2009-10-10T15:41:29-07:00
by Wilbert
Hmm, if i remove the DestroyImage(image); line everything works fine!!!

Is image automatically destroyed when images2 is destroyed?

Re: No memory released after reading one image

Posted: 2009-10-10T15:50:22-07:00
by magick
You destroy image and images2 which are aliased to each other so you are destroying the same image memory twice which is a bad-bad thing.