[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: The life cycle of userdata values?
- From: Marc Balmer <marc@...>
- Date: Mon, 25 Jul 2016 20:56:55 +0200
So..
> Am 25.07.2016 um 17:04 schrieb Marc Balmer <marc@msys.ch>:
>
> I am hunting a ghost. This is my ghost:
>
> Previously, we used the following construct in C code:
>
> p = malloc(sizeof int)
> *p = 42
>
> /* Do a Lua API call that might error (longjmp) out */
>
> free(p)
>
This leaks memory, if the API call errors out.
> p = lua_newuserdate(L, sizeof int)
> *p = 42
>
> /* Do a Lua API call that might error (longjmp) out */
This has the risk of the memory being invalidated by the
garbage collector.
> p = lua_newuserdate(L, sizeof int)
> l = luaL_ref(L, -1)
> *p = 42
>
> /* Do a Lua API call that might error (longjmp) out */
>
> luaL_unref(l)
This would leak memory as well if the API call errors out,
because the userdata is still referenced.