luaaa icon indicating copy to clipboard operation
luaaa copied to clipboard

Pass userdata object as argument to __gc function?

Open bit-garden opened this issue 6 months ago • 1 comments

I'm currently working with Raylib which is written in C. It relies on the developer to manually unload resources.

In the example of Texture2d, I would like to define __gc to clean it up automatically when used from Lua, but the reference to the userdata object gets lost.

Example:

luaTexture2d.def("__gc", [](Texture2d& self){ unloadTexture(self);});

__gc is not called in this case unless no arguments are passed.

Unless I'm missing something. I would have to add a GC function to the struct and then bind it that way, but this would break on upstream updates.

I also tried adding the destructor through the .ctor function but this throws a type error.

bit-garden avatar Jun 29 '25 18:06 bit-garden