Memory leak with nested pushContexts?

Is this an SDK bug (1.12.3)?

My Lua project fills the Malloc map and dies if I use a pushContext within another pushContext. I can't let the context stack get higher than one!

This main.lua shows my problem clearly—I'm not even doing anything with the empty images: pushContext (or lockFocus) alone is enough to crash. (And I'm reusing the same image variables every cycle, as you can see.)

import "CoreLibs/graphics"

local pd <const> = playdate
local gfx <const> = pd.graphics
pd.display.setRefreshRate(10)

local pushContextTest1
local pushContextTest2

function pd.update()
	pushContextTest1 = gfx.image.new(1000, 1000, gfx.kColorBlack)
	gfx.pushContext(pushContextTest1)
	
		pushContextTest2 = gfx.image.new(1000, 1000, gfx.kColorBlack)
		gfx.pushContext(pushContextTest2)
			
		gfx.popContext()
		
	gfx.popContext()
end

The dimensions of the images determine how fast memory fills up. (my project needs 1600x880, but even smaller images will slowly leak.)

Any workarounds?

Definitely a bug. I'll check it out in the morning and make sure it's fixed in the next update. Thanks for catching this! :pray:

5 Likes

Great! I've worked around it for my needs in the meantime—I could make do without nesting.

found it! I had an extra retain when the target image gets pushed to the stack, instead of handing the retain over. :confused:

I sure hope I haven't just changed this into a crash somewhere else.. :crossed_fingers:

3 Likes