lua-users home
lua-l archive

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


Hello All -

I am doing a very simple test of the tolua-5 gc engine involving variables
passed by value.  I have a method that returns an object.  toLua, as
expected, creates a new instance of this object via copy constructor and
passes a pointer onto the stack.

However, I have been completely unable to get the gc to automatically
clean up this created temporary object.  I would expect the following
snippet to result in the deletion of the temporary object that toLua
creates:

   v = make_Foo()
   v = nil
   collectgarbage()

However, this does not result in toLua's temporary Foo object being
destroyed.

I've included source for all the relevent files and the resulting output. 
As you can see, the temporary object is not destroyed by the call to
collectgarbage().  Fortunately, it /is/ destroyed when the interpreter is
closed, but this is much too late.

I am using vanilla tolua-5.0 and lua-5.0.2 on a linux 2.6.1 platform.

Suggestions, comments, advice? :)
--Benn

P.S.  In reality, I'm using lots of shared_ptr's, but this is a simplified
test case.  So no emails regarding the evils of passing-by-value, 'kay? :)

#  stdout.log ---------------------------------
Foo::Foo() = 0x000285C0
~Foo() = 0x000285C0
Done with test 1

Foo::Foo() = 0xCFBFD480
Foo::Foo(0xCFBFD480) = 0x000285C0
~Foo() = 0xCFBFD480
Done with test 2

~Foo() = 0x000285C0
#  eof ---------------------------------

#  test.lua ---------------------------------
f = Foo:new()
f:delete()
collectgarbage()
print("Done with test 1\n")

v = make_Foo()
v = nil;
collectgarbage()
print("Done with test 2\n")

#  eof ---------------------------------

#  foo.pkg ---------------------------------
$#include "foo.h"

class Foo
{
   Foo();
   ~Foo();
   int i;
};

Foo make_Foo();
#  eof ---------------------------------

#  main.cpp
extern "C"
{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}

#include "tolua.h"

#include "foo.h"
#include "foomap.h"

Foo make_Foo() { return Foo(); }

int main()
{
   lua_State *L = lua_open();
   luaopen_base(L);
   luaopen_io(L);
   tolua_foo_open(L);

   lua_dofile(L, "test.lua");

   lua_close(L);

   return 0;
}
#  eof ---------------------------------

#  foo.h ---------------------------------
class Foo
{
public:

   Foo() { printf("Foo::Foo() = 0x%08X\n", this); }
   Foo(const Foo &f) { printf("Foo::Foo(0x%08X) = 0x%08X\n", &f, this);}
~Foo() { printf("~Foo() = 0x%08X\n", this); }
   int i;
};

Foo make_Foo();
#  eof ---------------------------------

#  build.sh ---------------------------------
tolua -H foomap.h -o foomap.cpp foo.pkg
g++ -Itolua-5.0/include -Ilua-5.0.2/include -o test.bin main.cpp
foomap.cpp -Ltolua-5.0/lib -Llua-5.0.2/lib/ -llua -llualib -ltolua #  eof
---------------------------------