BrianL | 27 Sep 20:05
Favicon

lua_setallocf question

This seems like a really basic question - I'm probably missing some documentation or
discussionsomewhere. If so, please point me at it as I couldn't find anything on this specific issue:

If I install a custom allocator as follow:

    lua_State* state = luaL_newstate();
    lua_setallocf(state, &CustomAllocator::lua_Alloc, &alloc);
    lua_close(state);

...I get zero allocations (expected, as I do no work before destroying the state) but I get several
deallocations from allocations performed in luaL_newstate. This isn't safe given a custom allocator
that stores a header/footer for allocations, etc as it will cause memory corruption or memory leaks.

What is the correct way of handling this? I could restore the default allocator before calling
lua_closestate it seems like that would still have the same ambiguity issues (ie allocations performed
with the custom allocator could be freed to the default lua allocator).

Thank you!

Alex Davies | 27 Sep 20:24
Favicon

Re: lua_setallocf question

>    lua_State* state = luaL_newstate();
>    lua_setallocf(state, &CustomAllocator::lua_Alloc, &alloc);
>    lua_close(state);

luaL_newstate is from the aux library, use lua_newstate() instead.

- Alex

BrianL | 27 Sep 20:39
Favicon

Re: lua_setallocf question

Exactly what I was looking for, thank you!

I'm just starting with Lua embedding, but what use does lua_setallocf have? It seems extremely dangerous
in general due to the potential allocator mismatch issue.

--- On Sat, 9/27/08, Alex Davies <alex.mania <at> iinet.net.au> wrote:

> From: Alex Davies <alex.mania <at> iinet.net.au>
> Subject: Re: lua_setallocf question
> To: "Lua list" <lua <at> bazar2.conectiva.com.br>
> Date: Saturday, September 27, 2008, 11:24 AM
> >    lua_State* state = luaL_newstate();
> >    lua_setallocf(state,
> &CustomAllocator::lua_Alloc, &alloc);
> >    lua_close(state);
> 
> luaL_newstate is from the aux library, use lua_newstate()
> instead.
> 
> - Alex

Alex Davies | 27 Sep 21:07
Favicon

Re: lua_setallocf question

> Exactly what I was looking for, thank you!

Np.

> I'm just starting with Lua embedding, but what use does lua_setallocf 
> have? It seems extremely dangerous in general due to the potential 
> allocator mismatch issue.

Not much, but it's a simple function to be implemented that may be useful 
for some people. ie, you could use a regular allocator for most allocs, but 
on a lua function call swap in a tracing garbage collector which reports 
when memory is resized/freed etc. As long as both allocators are compatible, 
no harm in swapping them. Similarly you may keep the current allocator but 
merely change the UD pointer (which may not be a pointer at all, perhaps 
just an enumeration typecast to a pointer). Off the top of my head.

- Alex 

Re: lua_setallocf question

> I'm just starting with Lua embedding, but what use does lua_setallocf
> have? It seems extremely dangerous in general due to the potential
> allocator mismatch issue.

Besides Alex's suggestions, another use is to insert probes around
the original allocator. But certainly it is an "extremely dangerous"
function.

-- Roberto


Gmane