[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Userdata finalization order
- From: Petri Häkkinen <petrih3@...>
- Date: Sun, 17 Oct 2010 12:17:47 +0300
On 15.10.2010, at 23.58, Wesley Smith <firstname.lastname@example.org> wrote:
On Fri, Oct 15, 2010 at 10:48 PM, Petri Häkkinen <email@example.com>
The reference manual says:
"At the end of each garbage-collection cycle, the finalizers for
userdata are called in reverse order of their creation, among those
collected in that cycle."
Does this mean that sometimes userdata may not be collected in
reverse order, i.e. when objects are collected in different cycles?
If this is the case, writing Lua bindings for complex C++ libraries
just become more difficult in my mind. For example, there are
typically all sorts of managers, and dependencies between objects
that need to be teared down in proper order (reverse order of
I'm asking this because I just hit a case that causes a crash in a C
++ physics library when shutting down Lua. Basically there are a
few manager objects which get collected before some other objects
that have been created through the managers, and the physics
library doesn't like that at all.
What would be the recommended practice to deal with these situations?
You can't rely on userdata being collected in a particular order.
You'll have to implement some kind of notification system or improve
your collection logic when closing down a lua_State. If a manager
gets deleted, it should notify any clients. If a client gets freed,
it should detach itself from a manager. If you implement this kind of
logic, there shouldn't be any crashes.
Yep, that sounds sensible. It's just that tracking all those
dependencies is a lot of work (there are dozens of types/classes) and
the binding is already several thousand lines of C++ code so I was
hoping for an easier solution.