moai/moai-dev — pull request #769 by patrickmeehan (2013-09-03 10:22:57+00:00)
Title: Revisions to Moai Lua binding, memory management and garbage collection

This is just to get the discussion started. I revised the way Moai thinks about Lua GC to handle circular references quite a while ago, but there are still some edge cases that can crop up.

This particular pull requests restores the last 'good' behavior that avoids circular references and allows the GC to operate normally. I mistakenly added a call to MakeStrong in MOAILuaObject::OnRetain (), which fixed a sporadic crash I was seeing but inadvertently reintroduced circular references.

The problems stem from the divergence of the engine's internal ref counting sematic and Lua's GC. To avoid circular references, the engine's Release/Retain is decoupled from the Lua GC - Lua can collect an object but the engine can hang on to it. Likewise, the engine can be done with an object but Lua will hang on to it.

I would rely entirely on the Lua runtime entirely for managing references between objects, but performance is a concern. There are cases where the engine will want to ensure an object exists (via Retain) and not have to go out to the Lua runtime every time it needs access.

I think there are some intermittent crashes where an object gets GC'd but is kept around by the engine which then expects the object's Lua-side references to still be valid. For example, the object might have a callback that the engine needs to invoke, but if the callback was also collected then its ref will be stale. The ref will *appear* valid to the engine and (worse) may have already been re-purposed to refer to something else leading to sporadic and hard to debug crashes.

We can keep the current system, but the every use of MOAILuaRef, MOAILuaLocal and Release/Retain needs to be audited. It is also not safe to take the bool value of a MOAILuaLocal or a weak MOAILuaRef at face value as these objects may have been garbage collected and the ref number reused. Probably weak MOAILuaRef's should be avoided altogether and MOAILuaLocal's should never be used if their owner has been flagged for garbage collection.

I'm pushing this fix (and comments) mainly for the discussion. Will be doing more work on this in the coming days.