Page 1 of 1

[QT]about canvas:getImageData() and memory usage

Posted: Tue Oct 29, 2013 2:33 pm
by poorenglish
I use canvas:getImageData() in the function love.mousepressed(x,y,button),found the memory usage will increase too faster,it seems the memory will not be release.

Code: Select all

-- the image size is 1024*768,the app size is 1024*768 too

function love.load()
	assert('pixeleffect'), 'Pixel effects are not supported on your hardware. Sorry about that.')

	effect = [[
		extern Image img;
        vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords)
			vec4 pixel1 = Texel(img,texture_coords);
			vec4 pixel	= Texel(texture, texture_coords);
			if (pixel1.r >0)
				pixel.a = 0;
		return pixel;

	ColourBackGround ="back.png")
	BlackWhitebackGround ="backwb.png")
	Mask ="amask.png")

	canvasMask =,768)

	--	canvasMask:clear()

	canvas =,768),0,0)


function love.draw(), 0, 0), 0, 0), 0, 0)
--]] 'fps: ', 10,10 )

function love.mousepressed(x,y,button)
	local data = canvasMask:getImageData()
	Mask =

Is this the wrong method for canvas:getImageData()

Re: [QT]about canvas:getImageData() and memory usage

Posted: Tue Oct 29, 2013 6:26 pm
by Boolsheet
It will be released, eventually. The garbage collector will run and remove it once Lua hits a certain memory limit.

Lua doesn't know about the size of the raw data of the ImageData. All it knows is the size of the userdata which is a few bytes. You can create quite a few ImageData before the garbage collector runs. If you know you have garbage that you want to be removed now, call [manual]collectgarbage[/manual]("collect") to force a full cycle and remove everything that isn't referenced anymore.


Code: Select all

This is not necessary. renderTo already selects the canvas as a render target for you.

Re: [QT]about canvas:getImageData() and memory usage

Posted: Sat Nov 02, 2013 1:58 pm
by poorenglish
Sorry,too late for replying,thanks