Freeing memory from a canvas

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
illteteka
Prole
Posts: 3
Joined: Wed Jul 03, 2019 12:37 am

Freeing memory from a canvas

Post 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
User avatar
slime
Solid Snayke
Posts: 2958
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Freeing memory from a canvas

Post 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.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 62 guests