Prevent class from being garbage collected

,

I have a single GameManager created in the main.lua.

local pd <const> = playdate
local gfx <const> = pd.graphics

GameManager()

function pd.update()
    gfx.clear()
    gfx.sprite.update()
end

I think it's being garbage collected as I invoke garbagecollection() when the menu screen closes and levels finish and, when I do, I get this error:

Update error: main.lua:16: Sprite doesn't have a lua proxy!
stack traceback:
	[C]: in field 'update'
	main.lua:16: in function <main.lua:14>

I'm assuming that this is due to GC, and that it's telling me that the GameManager isn't there anymore.

If, on the line where you create the GameManager, you don't actually assign it to any variable, there aren't any references to it in Lua and it's subject to garbage collection.
An easy fix is to make a local variable within main.lua, e.g. local gameManager = GameManager(), though you have to make sure this variable never goes out of scope.

I tried that, same issue (I'm assuming it's due to GC).
I'll try writing a singleton instead, that might keep it alive.