Memory leak in MagickAnnotateImage

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
User avatar
franklin.amormino
Posts: 5
Joined: 2012-08-15T05:46:53-07:00
Authentication code: 67789
Location: Porto Alegre - Brazil

Memory leak in MagickAnnotateImage

Post by franklin.amormino »

I am developing an application to capture images from a webcam 3-4 Hz.The processing and written image using the functions MagickConstituteImage and MagickWriteImage are occurring correctly, but when using the function MagickAnnotateImage see what is happening a memory leak.
After a few minutes writing some images there is a break in the system.

Syntax I'm using:
MagickAnnotateImage (mw_destination, dw, 0,image_height, 0, subtitle);
where:
mw_destination is a MagickWand *;
dw is a DrawingWand *;
image_height is a size_t;
subtitle is a const char *;

I am using ImageMagick-6.7.8-Q8.
Does anyone have any information that can help me?
Thank you.
Franklin M. Amormino
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Memory leak in MagickAnnotateImage

Post by magick »

Post a small complete code set that we can use to reduce the leak. We'll download your code, compile, and run it against a memory debugger to track down any potential leak. Thanks.
User avatar
franklin.amormino
Posts: 5
Joined: 2012-08-15T05:46:53-07:00
Authentication code: 67789
Location: Porto Alegre - Brazil

Re: Memory leak in MagickAnnotateImage

Post by franklin.amormino »

I created a small test project in C + + Builder XE2 Update 4, I'm working on my project with C + + Builder. COFF2OMF used to convert the library CORE_RL_wand_.lib. My system is a Windows 7 Professional SP1.

I see that the number of handles increases very fast and the memory used by the process increases.

This is the little code.

Code: Select all

#pragma hdrstop
#pragma argsused
#include <tchar.h>
#include <stdio.h>
#include <windows.h>
#include <string>
#include "wand/magick_wand.h"
#pragma comment( lib, "CORE_RL_wand_.cbuilder.lib" )

#define array_len(v) (sizeof(v)/sizeof((v)[0]))

int save_picture(const char * output_picture_path, const char * subtitle)
{
	int result = 0;
	MagickWandGenesis();
	MagickWand * mw_destination = NewMagickWand();
	DrawingWand * dw = NewDrawingWand();
	PixelWand * pw = NewPixelWand();

	if(( MagickWand * ) NULL != mw_destination) {
		size_t image_width = 640;
		size_t image_height = 480;
		PixelSetColor(pw, "white");
		if( MagickTrue != MagickNewImage( mw_destination, image_width, image_height + 100, pw ) ) {
			result = 1;
		} else {
			DrawSetFontSize(dw, 35);
			PixelSetColor(pw, "black");
			DrawSetFillColor(dw, pw);
			//Annotates the image with text.
			if (MagickTrue != MagickAnnotateImage( mw_destination, dw, 0, image_height + 50, 0, subtitle )) {
				result = 1;
			} else {
				if( MagickTrue != MagickWriteImage( mw_destination, output_picture_path ) ) {
					result = 1;
				}
			}
		}
		DestroyDrawingWand(dw);
		DestroyPixelWand(pw);
		DestroyMagickWand(mw_destination);
	}
	MagickWandTerminus();
	return result;
}

int main()
{
	static const char * camera_test_bmp = "camera.test.bmp";
	SYSTEMTIME st;
	char tmp_out_text [ 1024 ];
	int result;
	printf("Processing...\n");
	while(true){
		GetLocalTime( & st );
		snprintf( tmp_out_text, array_len( tmp_out_text ),
			"%04d/%02d/%02d %02d:%02d:%02d.%03d",
			st.wYear, st.wMonth, st.wDay,
			st.wHour, st.wMinute, st.wSecond,
			st.wMilliseconds );
		if (save_picture(camera_test_bmp, tmp_out_text))
			break;
		Sleep(250);
	}
	return 0;
}
Watching with Process Explorer - Sysinternals, I could conclude that each handle is equivalent to an open registry key.
Have opened hundreds of registry keys:
    HKLM \ SOFTWARE \ Microsoft \ Windows NT \ CurrentVersion
Franklin M. Amormino
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Memory leak in MagickAnnotateImage

Post by magick »

We tried valgrind, a memory debugger under Linux, and it does not reveal any leaks. The only method that is Windows specific when annotating is NTLoadTypeLists(). So far we can't spot a leak when we inspect this method but we'll investigate further. In the mean-time, try moving MagickWandGenesis() / MagickWandTerminus() from save_picture() to main(). That way it is only called once, instead of once for each picture. Does not stop the leak?
User avatar
franklin.amormino
Posts: 5
Joined: 2012-08-15T05:46:53-07:00
Authentication code: 67789
Location: Porto Alegre - Brazil

Re: Memory leak in MagickAnnotateImage

Post by franklin.amormino »

Yes! I deleted MagickWandGenesis()/MagickWandTerminus() and was apparently contained the leak.
I'll let run for a few hours so I can observe the behavior.
Thank you my friend.
Franklin M. Amormino
Post Reply