Potentially small memory leak in updateTimers

,

I believe I am seeing a small memory leak when using a repeated timer.

Also - general apologies that I am still relatively new to lua, and if this report is off base. I mostly wanted to report something I was seeing, understood if it's not a big issue/priority.

Lua SDK - 2.6.2 on Mac - reproducible on playdate device.

Minimal sample code of how I am using a timer:

    local foo = 1
    local timer = playdate.timer.new(50)
    local endedCallback = function(timer)
        foo += 1
    end
    timer.repeats = true
    timer.timerEndedCallback = endedCallback

And executing timers with playdate.timer.updateTimers() via playdate.update().

Malloc log:

Looking a bit at playdate.timer.updateTimers() in CoreLibs/timer.lua:

There are a couple local functions being created on each invocation of updateTimers() and for each timer, eg:

local function updateActiveTimer(timer)
...
for i = 1, #timers do
  local function callEndedCallback()

I wonder if those functions could be defined a single time, outside of updateTimers(). The latter (callEndedCallback) closes over timer so might be harder to extract.

I have ~dozens of timers with relatively short durations, so while this is a small thing in isolation, it can add up (eg ~100 of these a second).

That said, this isn't a major issue for me. GC can keep up just fine (so maybe memory leak isn't the right term? memory pressure?). But in general I am trying to keep my memory usage as clean/low as I can to stay ahead of GC issues creeping up, and so wanted to report this.

1 Like