No memory released after reading one image

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
Post Reply
Wilbert

No memory released after reading one image

Post 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?
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: No memory released after reading one image

Post 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.
Wilbert

Re: No memory released after reading one image

Post 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.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: No memory released after reading one image

Post by magick »

Every image created must be destroyed. Use DestroyImageList(images).
Wilbert

Re: No memory released after reading one image

Post 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'.
Wilbert

Re: No memory released after reading one image

Post by Wilbert »

Hmm, if i remove the DestroyImage(image); line everything works fine!!!

Is image automatically destroyed when images2 is destroyed?
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: No memory released after reading one image

Post 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.
Post Reply