[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: how does GC work with complex expressions using metamethods?
- From: Sam Roberts <vieuxtech@...>
- Date: Fri, 14 Aug 2009 16:29:34 -0700
On Fri, Aug 14, 2009 at 4:06 PM, Michael
Newberry<mnewberry@mirametrics.com> wrote:
> Right, my classes are tables, declared generally like this:
>
> CData = { Cptr = nil }
>
> function CData:new()
> local o = {}
> setmetatable(o,self)
> self.__index = self
> o.Cptr = CreateData()
> return o
> end
>
> When the user uses A = CData:new() the registered function "CreateData" is
> called and it creates the C++ object aand returns a pointer to it. Class
> methods use o.Cptr to talk to the C++ object.
This isn't going to work.
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 can think of ways of getting around this, but they are unstable
hacks, and besides, userdata was designed to do exactly what you are
doing: encapsulate a C/C++ object/pointer, and give you hooks into
garbage collection so you can manage their lifecycle.
Cheers,
Sam