[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Lua C API can't be used from other languages without C wrappers
- From: Pierre Chapuis <catwell@...>
- Date: Tue, 20 Feb 2018 11:00:43 +0100
On Mon, Feb 19, 2018, at 20:29, Tim Hill wrote:
> I find it interesting that PiL is totally silent on this subject, and
> somewhat worrying since without either of the above strategies, code
> will silently leak resources, a type of bug that typically only shows up
> under stress (e.g. low memory) and is very hard to isolate.
PiL is not silent on the subject, it advocates your second solution.
It is chapter 27.3 of PiL 4, for instance.
There are two caveats, however:
- The solution only works since Lua 5.2, because afaik before Lua 5.2
lua_pushcfunction allocated memory. lua_cpcall had to be used
- Several code examples in PiL will leak resources if allocation fails,
e.g. the readdir example in listing 29.1 will leak the directory handler.
That being said, it depends on your context, but on a lot of modern
OSs there is nothing much to do if allocation fails, so defining a custom
Lua allocator or just using lua_atpanic may be enough if you control
your Lua code. If you write a sandbox however, there is no way to avoid
dealing with this
To be honest I have dealt with languages that use a different approach
to bindings such as Ruby and Python, and I still prefer the Lua solution.