Resources are never unloaded automatically #4
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 struct returned by
Load*orGen*is raw cdata with no lifetime. Forget oneUnload*and VRAM or RAM leaks until the process exits. The obvious fix — a
__gconTexture2D— doesnot work, but a per-object finalizer does.
Why the obvious fix fails
__gcon a struct type only runs for values created byffi.new. A struct returned by a Cfunction never gets a finalizer (
lib_ffi.c:511-524). Type-level__gcwould also double-freeevery copy (
ffi.new("Texture2D", tex)) and every borrowed view (font.texture,renderTexture.texture,model.meshes[i]), because one policy cannot tell an owner from a view.ffi.gcis the escape hatch: it arms any cdata, including a struct or pointer returned by C,returns the same value, and
ffi.gc(x, nil)disarms it. Verified against this repo's LuaJIT witha struct-returning libc call.
Proposal
1. Arm the exact value that C returned
Use
ffi.gcon the returned cdata. It attaches a finalizer to that specific value and returns itunchanged, so the API still returns
raylib.Texture2Dand every call site keeps working:Use raylib's readiness queries, not Lua-side flags: they stay correct when the window is managed
through
M.C. Leaking at teardown is deliberate — unloading a texture afterCloseWindowcrashes.2. Own by type, with explicit exceptions
A return is owned if its struct type is in an asset table (
Image,Texture2D,RenderTexture2D,Shader,Mesh,Model,Material,Font,Wave,Sound,Music,AudioStream,AutomationEventList,FilePathList,VrStereoConfig). That covers futureLoad*functionswithout a function list.
Exceptions, all explicit:
GetShapesTexture,GetFontDefaultLoadSoundAliasUnloadSoundAlias(shares sample data with the source)LoadModelFromMesh(mesh)meshfirst; the model owns itGenImageFontAtlasFail generation when an owned type resolves no unloader, or when a borrowed name is in the owned
set. Add
--dump-ownershipso the map can be reviewed on every raylib bump.3. Make explicit unload safe
Unload*disarms the finalizer, unloads, and zeroes the struct so a second unload is a no-op:Never zero a pointer result:
ffi.fillon a pointer writes through it.4. Keep C-retained values alive
These store a struct after returning, so the Lua object must not be collected:
SetShapesTexture,SetMaterialTexture,SetAutomationEventList. Pin the argument in a strong list. Pinning leaksuntil process exit; that is acceptable and bounded.
SetAutomationEventListalso has an existing bug: the wrapper passes a temporaryAutomationEventList[1]that raylib stores a pointer to. It dangles today and must be pinned.5. Pointer results
Array returns (
LoadImagePalette,LoadFontData,LoadModelAnimations,LoadMaterials) arearmed the same way, capturing the count in the closure (
MemFreeforLoadMaterials, which hasno unload function).
bytes/lines/intsresults from issue 04 are copied and freed immediately,so no finalizer is needed.
Acceptance criteria
GetShapesTexture/GetFontDefaultsurvive GC.GenMeshCube→LoadModelFromMesh, mesh dropped: no double free.LoadSoundAliasdropped: the source sound is still valid.CloseWindow(), drop, GC, exit: status 0.make check-bindingspasses.Notes
M.Ccalls stay raw and never arm finalizers.ffi.new; the copy would unload the original's id.Soundstops it. Keep a reference to keep playing.hnd_Texture2D) break drop-in usage; a strong live-resource registryprevents every finalizer; rewriting
Load*→Unload*by name fails on*FromMesh,*FromImage,Gen*andLoadSoundAlias.garbage collection hooksto Resources are never unloaded automatically