lua-users home
lua-l archive

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


Thanks! I'll take a look.

Michael

----- Original Message ----- From: "Sam Roberts" <vieuxtech@gmail.com>
To: "Lua list" <lua@bazar2.conectiva.com.br>
Sent: Friday, August 14, 2009 11:30 PM
Subject: Re: how does GC work with complex expressions using metamethods?


Maybe I'm not understanding what you are saying here, but what I described
has worked for years. But it requires the user call CData:delete(). So it
isn't automatic. Is it "not being automatic" that you mean isn't going to
work? If so, yes, that is the problem.

Requiring a user to call :delete() is a problem, IMO. Being able to
write code in a gced language that leaks memory is not so great.

Anyhow, your specific example involves expressions where there are unnamed
temporary variables created by operator overloading metamethods. The
user never has a ref to them, so can't manually delete them, and __gc
isn't called on tables, so you can't automatically delete them. So,
you can't do what you are trying to do this way.

CreateData needs to return a (full) userdata. That userdata will
probably contain nothing but your c++ pointer, and must have a
metatable, with a __gc function that will delete that pointer.


I already tried to do that, but maybe I goofed something. Here was the
code inside the Cfunction CreateData():

// ... create the C++ object with pointer pData

CData** ptr = (CData**)lua_newuserdata( L, sizeof(CData*) );
*ptr = pData;
return 1;

Back in the lua new() method, the value of self.Cptr has "something". But
what is that something? And how do I attach a __gc metamethod to it?

You use lua_setmetatable() to attach a __gc metamethod.

The io lib is a good example:

http://www.lua.org/source/5.1/liolib.c.html

newfile() shows how to do it.

createmeta() creates the meta table used in newfile().

This is described here in more detail:

http://www.lua.org/pil/29.html

Sam