Does Bah.Magick has a memory leak?

BlitzMax Forums/Brucey's Modules/Does Bah.Magick has a memory leak?

SLotman(Posted 2016) [#1]
This is the code I'm using to convert several images - I load the image with LoadMagickImage and pass the resulting TMImage into this function:



This is taking loads of memory, and never going down. Ran the program 3 times in a row, and got myself over 1GB of memory consumption. You can see I even tried adding a GCCollect in the function hoping it would take the memory leak down.

Then, I just added a "img.zoom("1x1")" after img.WriteFile.
Memory consumption went WAY LOWER. There is still a memory leak, but it never reaches even 50mb!

Is this something to do with Blitzmax GC? Or is it a leak in Bah.Magick itself?


SLotman(Posted 2016) [#2]
Added to common.bmx:
	Function bmx_magick_image_free(img:Byte Ptr

Added to glue.cpp:
	void bmx_magick_image_free(MaxMImage * img);

and later:
void bmx_magick_image_free(MaxMImage * img) {
	delete img;
}


And on magick.bmx / TImage class
	Method free()
		If imagePtr Then 
			bmx_magick_image_free(imagePtr)
			imagePtr = Null
		End If
	End Method


Now I don't need the img.zoom("1x1") - I'm getting even better results by calling image.free()

But there is still some memory footprint - my app still gains around 0.7Mb each time I convert a bunch of images :P


SLotman(Posted 2016) [#3]
Here is a small sample showing the memory leak:
Framework BRL.glmax2d
Import bah.magick

Graphics 800,600,0

While Not KeyHit(KEY_SPACE)
	Cls
	DrawText "PRESS SPACE TO START",10,10
	Flip
Wend

Cls
Flip

For Local f:Int = 0 To 100
 	Local img:TMImage = New TMImage
 	img = LoadMagickImage("icongames.png")
 	img = Null
	GCCollect
	Delay 100
Next

While Not KeyHit(KEY_ESCAPE)
	Cls
	DrawText "PRESS ESC TO CLOSE",10,10
	Flip
Wend


With this, the program compiled starts with 33.3Mb in memory. After the 101 images loaded, it takes 42.2mb.

I then added a img.free() before the img = null in the code above, and re-ran the test. Result: 34.1mb after loading images.

So, I am still missing something that should be released from TMImage - or something somewhere else, I'm not entirely sure. But it is obvious that there is a memory leak somewhere :/


Derron(Posted 2016) [#4]
I did not dig in the code, but they have a "***cacheTreshold"-Function - maybe it does not get rid of that cache ?


bye
Ron


SLotman(Posted 2016) [#5]
Trying to set bmx_magick_image_cachethreshold() to any value gives me an access violation :(


SLotman(Posted 2016) [#6]
Yikes! Things are much worse than I tought. I converted my test image to BMP, and the memory consumption went to almost 100mb in the test above - even with my img.free() :(


Derron(Posted 2016) [#7]
(While on my way to create an issue on github I just saw, that Brucey was aware of this thread...)

Brucey just commited some changes to "bah.mod/magick.mod".
https://github.com/maxmods/bah.mod

Next to your change he also removes "blob"-data.


Maybe this solves your issue?

bye
Ron


SLotman(Posted 2016) [#8]
Yes! Brucey is the man :)

I don't know what kind of magic (heh) he did, but I can confirm it has zero leak now :)