Small memory leaks when resizing gifs with MagickWand

The MagickWand interface is a new high-level C API interface to ImageMagick core methods. We discourage the use of the core methods and encourage the use of this API instead. Post MagickWand questions, bug reports, and suggestions to this forum.
Post Reply
Cubbee
Posts: 1
Joined: 2012-07-08T03:39:07-07:00
Authentication code: 13

Small memory leaks when resizing gifs with MagickWand

Post by Cubbee » 2012-07-08T03:43:19-07:00

Hello. I`m trying to resize animated gifs with ImageMagick MagickWand, but get some small memory leaks. ~3 Mb per 400 gifs. I`m not 100% sure if I`m using MagickWand right.

Code: Select all

MagickWandGenesis();
    MagickCore::MagickWand *magick_wand = NewMagickWand();
    MagickCore::MagickWand *magick_wand2 = NewMagickWand();
    
    for(std::vector<string>::iterator it = gifs.begin(); it != gifs.end(); ++it)
    {	
        inputFileName = "C:\\Projects\\gifs\\" + *it + ".gif";
        outputFileName = "C:\\Projects\\gifs\\out\\" + *it + ".gif";
		
        MagickReadImage(magick_wand, inputFileName.c_str());
		
        int width = MagickGetImageWidth (magick_wand);
        int height = MagickGetImageHeight (magick_wand);
                                        
        magick_wand2 = MagickCoalesceImages(magick_wand);
        ClearMagickWand(magick_wand);
		                            
        if(width > THUMBNAIL_WIDTH)
        {
            height = static_cast<int>(height * THUMBNAIL_WIDTH / width);
            width = 144;
        }

        if(height > THUMBNAIL_HEIGHT)
        {
            width = static_cast<int>(width * THUMBNAIL_HEIGHT / height);
            height = 144;
        }

        MagickResetIterator(magick_wand2);
        while (MagickNextImage(magick_wand2) != MagickFalse)
            MagickResizeImage(magick_wand2, width, height, MitchellFilter, 1 );
                    
        magick_wand = MagickOptimizeImageLayers(magick_wand2);
        ClearMagickWand(magick_wand2);
		
        MagickWriteImages(magick_wand, outputFileName.c_str(), MagickTrue);		        
        ClearMagickWand(magick_wand);
    }

	DestroyMagickWand(magick_wand);
	DestroyMagickWand(magick_wand2);
	MagickWandTerminus();
I`ve tried to use one MagickWand instead of two, but then functions don`t work properly and leaks are even higher. I`ve also tried to initialize MagickWand inside the for-loop, but it didn`t help. With this code gifs are resized and optimized properly. Memory leaks where detected by _CrtDumpMemoryLeaks(). They are also noticable with Windows Task Manager.

Thank you for any assistance.

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

Re: Small memory leaks when resizing gifs with MagickWand

Post by el_supremo » 2012-07-08T09:11:54-07:00

The only thing I can suggest is to try replacing each occurrence of ClearMagickWand(w) with
DestroyMagickWand(w);
w = NewMagickWand();

to see if it makes any difference.

Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.

Post Reply