weak_ref issue

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

weak_ref issue

Max McGuire
I've discovered an issue in the implementation weak_ref.  The issue is due to the way the Lua garbage collector works. Namely, an element can be removed from a weak table in a garbage collection cycle without the __gc metamethod being called on the element being removed.  The result is that the weak reference connection between an wrap_base derived class and its object_rep can be lost.

Here's what happens:

object_rep A is created and wrap_base A stores a weak reference to it. This reference is stored in slot 10 of the weak table.


object_rep A is no longer in use an becomes garbage.

The garbage collector runs far enough to remove the reference to object_rep A in the weak table.

object_rep B is created and wrap_base B stores a weak reference to it. Slot 10 in the weak table is now empty so the reference
to B is stored in slot 10.

The garbage collector runs and invokes the __gc method for object_rep A. This destroys wrap_base A, which in turn destroys the weak_ref and calls luaL_unref on slot 10 of the weak table

Now wrap_base B now longer has a valid reference for object_rep B.

I've attached a simple program that demonstrates the behavior of the garbage collector in Lua.  Because I'm not very familiar with all of the details of luabind, I'm not sure how to proceed with fixing this issue. Any advice would be appreciated.



Technical Director
Unknown Worlds Entertainment

This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
luabind-user mailing list
[hidden email]

main.cpp (2K) Download Attachment