Memory Leaks while annotating image

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
Sebaestschjin
Posts: 1
Joined: 2015-08-11T07:26:32-07:00
Authentication code: 1151

Memory Leaks while annotating image

Post 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.
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: Memory Leaks while annotating image

Post by dlemstra »

This looks like our font type cache but that is released inside 'MagickWandTerminus'. I'll take a look at this later today.
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Memory Leaks while annotating image

Post 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)
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: Memory Leaks while annotating image

Post 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);
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
Post Reply