libRocket/libRocket — pull request #213 by Gambini (2014-09-07 06:46:10+00:00)
Title: Fix memory leak with ReferenceCountable objects

When document:CreateElement or document:CreateTextNode was called in Lua, the elements would never have their references removed. Now the references are properly accounted for.

Things I learned while I was fixing this:

The Lua plugin requires a different lifetime compared to what the plugin api offers. You must destroy all contexts before calling Rocket::Core::Shutdown. However, if you wait until then to shutdown Lua, then Elements created or used in Lua will still have reference counts > 0. The destructor of an element can destroy compiled geometry, which checks its parent context for the render interface (which will not exist if context is destroyed before Lua is shutdown).

In short, make sure you shut down Lua before you close contexts for now. In a later update, I am considering making one Lua State per document (similar to javascript in html), but I have to look in to the unloading order and reference counting there a little deeper to see if it will fix that.