lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

On Sun, Sep 12, 2004 at 10:37:43PM -0500, Matthew Harmon wrote:
> Thanks... I did have a handle on that, I should have been more clear.  The
> child state definitely got GCed as you said.
> I guess what I'm confused about is this... should doing my
> luaL_ref(childState, LUA_REGISTRYINDEX) with the entry point be enough to
> prevent GCing of the childState?  Since the reference is "within itself",
> does that count?  It seems so because my memory usage keeps creeping up if I
> don't luaL_unref().  Yet... sometimes one get's GC'd and causes that crash.

You're confusing the two separate things which you need to keep track

- The child state itself. This is the object that is pushed onto the
  stack as a result of calling lua_newthread. Even though you have a
  C-side pointer to this object (a lua_State), it is still subject to
  being GCed if you don't save the 'Lua half' of it somewhere, at
  which point the C-side pointer will be left dangling, hence the
  crash. Doing a luaL_ref on that object will indeed preserve it, but
  it doesn't sound like you're doing that. So, this is being GCed. You
  need to add refs and unrefs for this object.

- The script which will be run in the child state. You're saving a
  reference to this, and it won't be GCed unless you unref it again
  /even if the state it was going to run in has been GCed/. Hence the
  creeping memory usage you saw. You're treating this object
  correctly, but seeing problems when you try to unref it because of
  the above.

-- Jamie Webb