danomatika/ofxLua — pull request #7 by totalgee (2013-08-30 09:51:30+00:00)
Title: Fixed leak in ofxLua::exists() -- Lua stack grew each time it was called

The Lua stack leak was caused by a missing pop in `ofxLua::exists()`. This was occurring in the "normal" use case: when there are no pushed tables. Basically, the Lua stack was exploding at a rate of about 120 elements per second, if you happened to be calling `scriptUpdate()` and `scriptDraw()` once per frame, at 60fps...! I also looked at your pushing/popping tables code (an ofxLua feature I'd never used before), but that seems to be okay (stack-leak-free).

> ### SIDE NOTE
> I'm not sure what behaviour you intended in the case where you've pushed tables and then call `scriptUpdate()` (or any of the other `script*()` methods). Right now, it does a test to make sure the function `update` exists, using `isFunction()`. So if you're inside a table, it likely doesn't exist. if it doesn't find the `update` function in the table, it won't do anything (i.e. won't call the global `update` function).

> However, if you **do** have an `update` function in your pushed table, it won't get called, either! If you have one, then the `isFunction()` test succeeds, but `scriptUpdate()` proceeds to call the **global** `update` (and probably fails or crashes if there isn't one in the global context). What did you intend in this case? (admittedly, it's probably not super important for most people, but just thought I'd flag it)

I also added a local debugging class, `LuaDebugStack`, which traces the Lua stack size when it comes into and leaves scope. To use, add this at the top of a method, or at any level of scope you want to examine:
```c++
void ofxLua::someMethod() {
LuaDebugStack debug(L, "someMethod");
// ...some pushing and popping, Lua-fiddling, etc., which we want to debug
// and that we expect to leave the stack unchanged...
}
```
If you prefer, I can take the declaration of this class out (it's not used in these committed changes). But it could be handy when debugging similar things in the future.