lua-users home
lua-l archive

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


I'm trying to get C++ objects created with tolua garbage
collected.  Setting the "gc" tag method from within Lua
seems like it's the right thing to do, and it works if
I do it "by hand".  But if I try to encapsulate that idea
into its own function, it fails.  I'm using Lua 3.2.  The
C++ objects are implemented using Tolua.  The C++ objects
print something like [alloc empty vec 0x........] when they
are created, and [destroy vec 0x........] when they are
deleted.

This seems like a bug to me, but maybe there is something
I'm missing.

Tom

Here is the Lua code:

======================================================================
use_makeobj = nil

print("start")

function delmethod (x)
    print("delmethod",x)
    x:delete()
end

print("delmethod is",delmethod)

function makeobj (allocator,args)
    result = call(allocator.new,args)
    settagmethod(tag(result),"gc",delmethod)
    print("tag",tag(result))
    print("gc tag method is",gettagmethod(tag(result),"gc"))
    return result
end

print("calling makeobj")

if use_makeobj then
    v = makeobj(Vec,{})
else
    v = Vec.new()
    settagmethod(tag(v),"gc",delmethod)
end
print("v is",v)
print("tag(v) is",tag(v))

print("gc1")
collectgarbage()
print("gc tag method is",gettagmethod(tag(result),"gc"))
v = ""
print("gc2")
collectgarbage()
print("after")
print("v is",v)
collectgarbage()
collectgarbage()
collectgarbage()
print("done")
======================================================================

Here is the output with use_makeobj=nil:



======================================================================
start
delmethod is	function: 0x806bd30
calling makeobj
[alloc empty vec 0x806afd0]
v is	userdata: 0x806afd0
tag(v) is	-13
gc1
gc tag method is	nil
gc2
delmethod	userdata: 0x806afd0
[destroy vec 0x806afd0]
after
v is	
done
======================================================================

And here is the output with use_makeobj=1:

======================================================================
start
delmethod is	function: 0x806bd30
calling makeobj
[alloc empty vec 0x806afd0]
tag	-13
gc tag method is	function: 0x806bd30
v is	userdata: 0x806afd0
tag(v) is	-13
gc1
gc tag method is	function: 0x806bd30
gc2
after
v is	
done
======================================================================