Page 1 of 1

Freeing memory from a canvas

Posted: Wed Jul 03, 2019 12:49 am
by illteteka
Hello, long time lurker, first time poster.

I'm working on a very simple art program in Love that uses a Canvas for modifying images. Every time the user starts the program, they can name the file and set the width and height of the image, this also creates a new canvas to fit the new project size. The problem I'm running into is, I'm used to surface_free(surface_id); in GameMaker, which will free the memory used by your surface (canvas), such that if you create one again, you don't have to worry about filling all the memory on your computer with canvases you no longer have access to. At the moment, every time you create a new image without restarting the program, I make the canvas nil and call the garbage collector. While it seems like this should work, the memory usage in Task Manager is... inconclusive at best. I'd like to know where I'm going wrong or if there's a better way to achieve this.

Thanks!

(Relevant code snippet)

Code: Select all

function artboard.init()

	-- Reset canvas and collect garbage
	artboard.canvas = nil
	collectgarbage()

	-- Make the w/h a power of 2
	local ax, ay = 2, 2
	while ax < document_w do ax = ax * 2 end
	while ay < document_h do ay = ay * 2 end
	
	artboard.w, artboard.h = ax, ay
	
	-- Create canvas
	artboard.canvas = lg.newCanvas(artboard.w, artboard.h)
	artboard.canvas:setFilter("nearest", "nearest")
	
	-- Clear canvas
	lg.setCanvas(artboard.canvas)
	lg.clear()
	lg.setCanvas()

end

Re: Freeing memory from a canvas

Posted: Wed Jul 03, 2019 1:35 am
by slime
If there are no other references to the Canvas, that should work. You might have to call collectgarbage() twice to cause every bit of unreferenced data to be completely freed though, I don't remember the details on that.

love also has Object:release, which sounds similar to surface_free.