lua-users home
lua-l archive

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


On Sat, Apr 7, 2012 at 2:18 AM, Miles Bader <miles@gnu.org> wrote:
> Rena <hyperhacker@gmail.com> writes:
>>> BTW, I should note that the way SWIG allocates objects like this is to
>>> use C++ "new" to create the actual object, and then allocate a Lua
>>> userdata containing a pointer to the C++ object, some info about its
>>> type, and a Lua metatable with a __gc entry that does the deallocation.
>>>
>>> I don't think it's particularly efficient for small objects like this,
>>> but as far I can tell it does the right thing, and the userdata in
>>> this case is about the same size as the referenced data, so Lua's GC
>>> should have roughly the right idea of how much memory is being used.
>>
>> A boxed pointer, in other words. I don't know of any other method to
>> deal with C++ objects that expect the use of new and delete.
>
> Many C++ objects work perfectly fine allocated in other ways though.
> Certainly if I was hand-rolling this code, I'd just use placement-new to
> construct the object inside the userobject itself.

You'd still need to use operator delete on it instead of free if you
need the destructor invoked.

/s/ Adam