[oolua] Re: C++/OOLUA Memory management

  • From: "apscience" <apscience@xxxxxxxx>
  • To: <oolua@xxxxxxxxxxxxx>
  • Date: Tue, 15 Oct 2013 08:42:18 -0700

Ok, this is exactly what I was looking for. I looked up traits in the 
documentation and found lua_acquire_ptr which worked. [1] However the example 
you linked does not seem to work. It seems those traits don’t exist for my 
version of OOLUA? As far as I know I’m using 2.0.0-Beta1 and I’ve been 
following the documentation labeled as such.

Is there separate documentation about these other traits? Or are they new 
enough that documentation is still pending?

[1] 
http://oolua.googlecode.com/svn/docs/branches/dsl/html/group___o_o_lua_traits.html

Thanks again!


From: liamdevine 
Sent: Tuesday, October 15, 2013 1:24 AM
To: oolua@xxxxxxxxxxxxx 
Subject: [oolua] Re: C++/OOLUA Memory management


On 15 Oct 2013, at 02:08, apscience wrote:


  Hello,

  I’m trying to use OOLUA for a project but I’m afraid I’m kind of stuck.


Hmm the names of the libraries are Lua and OOLua, OK now that is out of the 
way. :)


  Essentially I have C++ objects that are created in C++. I want to be able to 
pass them to lua for manipulation. However I don’t want the lua script to ever 
have references to destroyed objects. 

Well this situation is not much different from C++03, in that you can have 
dangling pointers where you delete one instance yet hold copies elsewhere. It 
is sometimes a mind shift to go from C++ memory management model to Lua's.


  The objects can be destroyed in lua or from within c++. Is there any way to 
go through all the lua state and set references that reference a destroyed 
object to nil? Or to recognize a passed reference is invalid and throw an error 
on the LUA side? (That way the C++ never tries to use an invalid poinetr)

As C++ itself does not have such a provision, it makes it hard for the library 
to provide this. The only way it happens, without shared pointers, in C++ is 
that debug runtimes will write a specific bit pattern to the pointer which is 
checked before usage. It is theoretically possible to traverse a vm and find 
all instances of objects in local scope, upvalues and global scope; where it 
maybe a value or a key in some table for example but that would be very 
expensive. Generally one language should own a pointer, but if C++ owns the 
pointer and you store references in multiple places in Lua which are not 
weak[1] then that will be a problem.



  My current approach is to create all the objects in lua and store them in 
arrays. When I want to destroy the object from C++ I remove the the object from 
the array in lua. Then when all the references are eventually destroyed the lua 
code automatically cleans up the object.

  Unfortunately this has a lot of down sides:
  * Relying a lot on the lua garbage collector
  * have to create/destroy everything using lua


Generally the Lua GC is very good, although most people will turn it off in 
high performance applications and run it when it is suitable for the programme 
in question. I will cover the other point below.


  I might be able to work around some of the down sides, but at the very least 
I really want to create the objects in C++. So I want to be able to have some 
code that looks like:
  object * MakeObject() {
      return new object(stuff);
  }

  and have the object* be memory tracked by LUA. Or if there’s a way to avoid 
this hacky approach altogether that would be appreciated.


Sure, this possible and the general solution. I am going to assume you are 
using OOLua 2 beta here. 
You want to use the Expressive DSL which allows for traits to be applied to 
types. Have a look at Traits in the manual, also see [2] which is a C++ 
function returning a pointer which will be owned by Lua and [3] which is the 
binding which informs the library of this.



  Thanks,
  Anthony



[1] http://www.lua.org/manual/5.2/manual.html#2.5.2
[2] 
https://bitbucket.org/liamdevine/oolua/src/f3d8f2ee639ed487fe6a090965de37b021316d0e/unit_tests/cpp_classes/cpp_function_returns.h?at=OOLua-2#cl-19
[3] 
https://bitbucket.org/liamdevine/oolua/src/f3d8f2ee639ed487fe6a090965de37b021316d0e/unit_tests/bind_classes/expose_userdata_function_returns.h?at=OOLua-2#cl-28

Other related posts: