Callbacks leak: nothing is ever unpinned #9
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
Every callback passed to raylib is pinned forever.
pin()appends to a global list and caches theclosure in a strong table. Nothing ever removes an entry, so replacing or detaching a callback
leaks both the Lua closure and the FFI trampoline.
Current behavior
gen-hate-no-d.lua:769-787emits:Consequences:
SetAudioStreamCallback(stream, other)keeps the previous closure and pointer pinned too.DetachAudioStreamProcessorcallspin()again (hate-no-d.lua:8775), so detaching addsreferences instead of removing them.
There are 9 callback parameters in the API; each has this problem.
Why it matters
Long-running programs grow without bound, and there is no way for a caller to release a callback.
The growth is invisible until memory or trampoline count shows up in a profile.
Proposal
Replace the two global tables with a slot registry.
"file:data"). Per-streamcallbacks key on
stream.buffer, which is stable and unique per stream.Set*Callback(fn)overwrites the slot;nilclears it.Attach*increments,Detach*decrements and returns the samepointer that was attached. Detaching an unpinned function still works, with no retention.
ffi.castresults are cached with__mode = "k"so an unused closurecan be collected.
CloseWindowclears all slots;M.callbacks.releaseAll()does it manually.Sketch of the registry:
Acceptance criteria
SetAudioStreamCallback(s, cb)twice leaves exactly one live entry.Attach/Detachin a loop does not grow memory; detach passes the pinned pointer.collectgarbage(), a dropped callback closure is collected.collectgarbage("count")flat.make check-bindingspasses.Notes
slotsmust holdfn; the closure has to outlive raylib's use of the pointer.#15