Mr. Strange wrote:You should put this in the "keypressed" function, rather than checking "isDown" - that will return true 60 times a second, and overflow your stack (apparently)
Uhhh... no. Code gets called 60 times per second (assuming vsync, etc) but returns each time. No stack problem.
Green_Hell wrote:That's why I use
That's just for debugging purposes, right?
Green_Hell wrote:
Problem is that I need to increase font size by holding the key and also be able to increase precisely by one click.
The program above is debug program not my real program.
Anyway.
Code: Select all
function keypressed(key)
if key == love.key_space then
-- Space to incrase font size
my_font_size = my_font_size + 1
my_font = love.graphics.newFont("DejaVuSans.ttf", my_font_size)
-- love.timer.sleep(50)
elseif key == love.key_escape then
-- Escape to exit
love.system.exit()
end
end
Works the same => eats memory.
Yeah, it does. I can get it to suck up a couple hundred MB of memory in under a second. So either there's a memory leak or garbage collection isn't happening. Nothing we can do about the first, but let's test the second...
Code: Select all
function load()
my_font_size = 12;
end
function update(dt)
collectgarbage("step");
if love.keyboard.isDown(love.key_lshift) or love.keyboard.isDown(love.key_rshift) then
if love.keyboard.isDown(love.key_period) and my_font_size < 128 then
my_font = nil;
my_font_size = my_font_size + 1;
elseif love.keyboard.isDown(love.key_comma) and my_font_size > 5 then
my_font = nil;
my_font_size = my_font_size -1;
end;
end;
if not my_font then
my_font = love.graphics.newFont(love.default_font, my_font_size);
end;
end;
function keypressed(key)
if key == love.key_escape then
love.system.exit()
elseif key == love.key_g then
collectgarbage("collect")
elseif not (love.keyboard.isDown(love.key_lshift) or love.keyboard.isDown(love.key_rshift)) then
if key == love.key_period and my_font_size < 128 then
my_font = nil;
my_font_size = my_font_size + 1;
elseif key == love.key_comma and my_font_size > 5 then
my_font = nil;
my_font_size = my_font_size - 1;
end;
end;
end;
function draw()
-- draw message
love.graphics.setFont(my_font);
love.graphics.draw("Font size is " .. my_font_size .. ".", 100, 100);
end
Sorry about changing the keys on you.
This runs a single step of the garbage collector each update, and that seems to fix the problem. Memory allocation still balloons when you're reallocating fonts 60 times per second, but it drops as the garbage collector works its way through the released objects.