lua-users home
lua-l archive

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


> Hi,
>
> I just got into an problem with tolua 3.2.2, which broke my code completely.
> I discovered that tolua allocates memory for userdata types in c++
> environments via the new function. But the memory is freed with the function
> free (in toluaI_tm_undoclone) and not the function delete. There is even a
> comment on that line that looks like the author knew what he did
> ("free(clone); /* no destructor: use raw free */").

Yes, I am aware of that, and I thought it would work correctly for
single inheritance (possibly with memory leak for complex objects).
Sorry for not pointing it out in the docs.

The correct approach however is not a matter of just including #define
__cplusplus.
The code receives a pointer to void and you can not call delete for it.

I see two approaches to overcome the problem:
1) tolua could require that all objects returned by value to Lua inherits from a
base class ToLuaBase,
which would define a virtual destructor.

2) tolua would generate, automatically, a "destructor_function" for each type
returned by value to Lua.

As tolua tries not to impose conditions to the code being mapped, the second
solution seems to be a better choice.
I think that would work nice, isn't it? Any ohter ideas?

-- waldemar