Page 1 of 1

Memory Leaks while annotating image

Posted: 2015-08-12T00:43:34-07:00
by Sebaestschjin
Hi,

I'm trying to use the ImageMagick API to generate an image from text, which will be used elsewhere. However when I use the function MagickAnnotateImage() my Visual Studio tells me about memory leaks when exiting the application. They look as follows:

Code: Select all

..\..\ImageMagick\magick\memory.c(169) : {113809} normal block at 0x07F0ABF0, 9 bytes long.
 Data: <Webdings > 57 65 62 64 69 6E 67 73 00 
..\..\ImageMagick\magick\memory.c(169) : {113800} normal block at 0x07F0A900, 20 bytes long.
 Data: <Verdana-Bold-Ita> 56 65 72 64 61 6E 61 2D 42 6F 6C 64 2D 49 74 61 
..\..\ImageMagick\magick\memory.c(169) : {113791} normal block at 0x07F0A600, 13 bytes long.
 Data: <Verdana-Bold > 56 65 72 64 61 6E 61 2D 42 6F 6C 64 00 
This list goes on for every font that exists on my system, so it seems to me, that some kind of font objects are not cleared correctly.

I built ImageMagick from source using version 6.9.1-10 and the configuration with the /MD flag and a Quantum depth of 8. I'm inlcuding the sources in my project and linking against the generated lib files. My system is Windows 7, 32 bit with Visual Studio 2010. As in the VisualMagick projects the default calling convention is set to __cdecl if that matters somehow.

This is the excerpt of my code that uses the ImageMagick API:

Code: Select all

...
MagickWandGenesis();

MagickBooleanType success;

CString text = "Hell World";

MagickWand* textImage = NewMagickWand();
PixelWand* pixels = NewPixelWand();
PixelSetColor(pixels, "red");
DrawingWand* drawing = NewDrawingWand();
DrawSetFontSize(drawing, 15);
DrawSetGravity(drawing, CenterGravity);
success = MagickNewImage(textImage, 200, 100, pixels);
success = MagickAnnotateImage(textImage, drawing, 0, 0, 0, text.GetString());
success = MagickWriteImage(textImage, "D:\\output.png");

// clean up
DestroyPixelWand(pixels);
DestroyDrawingWand(drawing);
DestroyMagickWand(textImage);
MagickWandTerminus();
...
The leaks don't appear if I don't use MagickAnnotateImage(). I also tried using the Magick++ header file and the C++ API, but with the same results.

Is there anything I am missing? The API itself works really great and is just what I need, but I don't want to use it, if I end up with all those leaks.

Thanks in advance for any help.

Re: Memory Leaks while annotating image

Posted: 2015-08-12T01:11:59-07:00
by dlemstra
This looks like our font type cache but that is released inside 'MagickWandTerminus'. I'll take a look at this later today.

Re: Memory Leaks while annotating image

Posted: 2015-08-12T03:23:38-07:00
by magick
We ran your code snippet under Linux and valgrind, a memory leak detector, with ImageMagick 6.9.1-10. It does not show a leak:
  • $ valgrind a.out
    ==42795== Memcheck, a memory error detector
    ==42795== HEAP SUMMARY:
    ==42795== in use at exit: 127,146 bytes in 4,055 blocks
    ==42795== total heap usage: 29,327 allocs, 25,272 frees, 11,504,478 bytes allocated
    ==42795==
    ==42795== LEAK SUMMARY:
    ==42795== definitely lost: 0 bytes in 0 blocks
    ==42795== indirectly lost: 0 bytes in 0 blocks
    ==42795== possibly lost: 592 bytes in 1 blocks
    ==42795== still reachable: 126,554 bytes in 4,054 blocks
    ==42795== suppressed: 0 bytes in 0 blocks
    ==42795== Rerun with --leak-check=full to see details of leaked memory
    ==42795==
    ==42795== For counts of detected and suppressed errors, rerun with: -v
    ==42795== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Re: Memory Leaks while annotating image

Posted: 2015-08-12T14:19:24-07:00
by dlemstra
I can confirm this leak and I just submitted a patch to our GIT repository. This is a Windows only bug that can be fixed by changing line 528 of nt-feature.c to:

Code: Select all

status=AddValueToSplayTree(type_cache,type_info->name,type_info);