lua-users home
lua-l archive

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


>
>
> the tolua docs are somewhat cryptic about this, so I just ask the list. We
> are currently doing a game with lua as script engine for animation
> definitions, user interface and other stuff. We use tolua for binding our
> game objects to lua. Now there are some questions about how tolua uses
> memory management and works together with the garbage collector. Since we
> currently use tolua 3.2, but probably switch to tolua 4.0 when it is ready,
> it would be nice if the questions are answered for both versions!
>
> 1. I create a c++ object from within lua, with something like
>
>         button = CMnuButton:new()
>
>   Will the appropriate destructor be called when button leaves the scope
> (ie. the GC gets it)?
>

no, it won't be collected. objects created with "new" should be explicitly
deleted with "delete".
or you may call the function "takeownership" to tell tolua to collect it when it
leaves the scope.

>
>
> 2. Now I register the object within my framework. From now on my framework
> should be responsible for this object, and the GC should NOT delete the
> userdata associated with button, when button is collected. Is there any
> chance to do something like this?
>
> Ex:
>
>   function Dialog()
>     local b1
>     local b2
>
>     b1 = CMnuButton:new()
>     b2 = CMnuButton:new()
>     Screen:RegisterButton(b1)
>     Screen:RegisterButton(b2)
>   end

again, it will not be garbage collected.

>
> 3. I have methods that return pointer to an object. Will this object stay
> untouched when the var where it is referenced by leaves the scope?
>

sure.

> 4. I see from the code, that tolua makes copies of userdata objects returns
> by reference or value. Are these objects correctly collected when they leave
> scope?

in that case, it will be collected. the behavior here is the same as when you
call "takeownership".

-- waldemar