Page 1 of 1

[solved] love.graphics.newImage causes memory leak in MacOS?

Posted: Mon Sep 08, 2014 8:54 am
by Sasha264
Hello everyone =)

I'm trying to create some textures from code, and noticed some strange behaviour.
Please look at [1] and [2] lines of my code. With image:refresh() [2] code works fine on Windows and on MacOS, but with love.graphics.newImage(...) [1] in Windows works fine, but in MacOS it causes memory leak and then crash love http://monosnap.com/image/n8oHMiVtuUFyi ... DnLzcEtUif
Note: I have love 0.9.1. Image:refresh() is the great method, but in some cases creating new images would be more convenient...
Can anyone help me please? (:

Code: Select all

function love.load()
	w = 512
	h = 512
	love.window.setMode(w, h, {resizable=false, vsync=false})

	imageData = love.image.newImageData(w, h)
	image = love.graphics.newImage(imageData)
end

function love.update(dt)
	for i = 0, w-1 do
		for j = 0, h-1 do
			imageData:setPixel(i, j, 255, 255, 255, math.random(0, 255))
		end
	end

	image = love.graphics.newImage(imageData) -- [1] memory leak?
	-- image:refresh() -- [2] works fine
end

function love.draw()
	love.window.setTitle("fps " .. love.timer.getFPS())
	love.graphics.draw(image, 0, 0)
end

Re: love.graphics.newImage causes memory leak in MacOS?

Posted: Mon Sep 08, 2014 10:29 am
by Robin
Welcome!

[wiki]love.graphics.newImage[/wiki] is one of the functions where it is documented that it is not a good idea to call them every frame, so that is expected behavior. I doubt the devs will see this as a bug, but if you want to you can report it on the issue tracker.

Re: love.graphics.newImage causes memory leak in MacOS?

Posted: Mon Sep 08, 2014 2:04 pm
by jjmafiae
as Robin said don't run this every frame and in the next version of löve (0.9.2) love.gfx.newImage uses less RAM.

Re: love.graphics.newImage causes memory leak in MacOS?

Posted: Mon Sep 08, 2014 4:19 pm
by bartbes
You are supposed to use refresh for this, to prevent creating lots of garbage, and it seems that your crash is simply a result of the garbage collection not being run fast enough. It is of course possible that there is a memory leak, but it's unlikely.

Re: love.graphics.newImage causes memory leak in MacOS?

Posted: Mon Sep 08, 2014 4:21 pm
by jjmafiae
to run garbage collector do this: collectgarbage()

But I have never seen anyone that actually needed to run love.gfx.newImage every frame.

Re: love.graphics.newImage causes memory leak in MacOS?

Posted: Tue Sep 09, 2014 6:28 am
by Plu
What are you trying to accomplish, anyway? There's probably a better way for it.

Re: love.graphics.newImage causes memory leak in MacOS?

Posted: Tue Sep 09, 2014 12:30 pm
by Sasha264
collectgarbage() solves the problem, and now on MacOS and on Windows memory consumption are the same.
So that was obviously not a memory leak.
Yes, I'm not going to call love.graphics.newImage every frame, only when I need it. This example was just for clarity.

Thanks for the help ^_^
Plu wrote:What are you trying to accomplish, anyway?
I want to try some kind of "procedural textures". Is here any links for examples in love?

Re: love.graphics.newImage causes memory leak in MacOS?

Posted: Tue Sep 09, 2014 1:13 pm
by Plu
Shouldn't the texture be more or less fixed after it's first render? I can only imagine that right now it's either constantly jittering because it looks a little different on each run.

I think you basic approach seems about right (except you should probably do it using a Shader because those are infinitely faster) but you shouldn't be redoing it every frame. You should draw the textures once and then just constantly redraw the same image.

Re: love.graphics.newImage causes memory leak in MacOS?

Posted: Fri Dec 01, 2017 7:11 pm
by Sasha264
Plu, yes you a right, shaders are proper place for this experiments :cool:

Anyway the initial problem was solved by the call of garbage collector.
Thanks amigos! :awesome: