I have a userdata "A" being referenced referenced by another userdata "B". "A" has the __gc metamethod that can be used to schedule its removal from "B", but note that the reference is not removed immediately, it is deferred until a certain function "C" is called(This is how the library I'm working with behaves). The problem is that if Lua frees "A" memory before "C" is called, "B" will be left with a dangling pointer that can cause memory errors when "C" is called later(It will be called for sure).
What I want to achieve is to defer "A" memory from being freed until C is called. Is there a way to do this? I know that one option is to work with userdata that simply references "A" and take care of freeing memory myself, but I'd rather avoid this because it would need a lot of refactoring in the project I'm working on.