I've been working on a Lua project that wraps a C++ interface. Included in the interface are two objects that are created with a parent-child relationship. If a reference to the parent disappears and Lua garbage collection reclaims the parent object, using the child object will cause things to blow up.

It took me a while to find an example of how to use a weak table to record these relationships that indirectly result in the correct GC policy. Here is the final patch for reference.

One open question here is what order does garbage collection impose between parent and child reclamation? Forcing a particular order may require some reference counts in the C++ objects. On the other hand, the C++ objects may not be sensitive to the issue, but this must be decided on a case-by-case basis.