lua-users home
lua-l archive

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

Hi All,

I have come across what i think might be a possibility for me to loose some system allocated data within lua C functions.

Before i delve into the problem i see i shall first mention that i am using Lua 5.1.1 and the target language is C, C++ is not an option.

Basically the simplest explanation i can give is that i have a lua script calling a c level function that allocates data to do processing on passed from lua, this gets allocated on the system heap (no way around this, its a library i don't have source access to), i then perform the process and then start to pass the results back to the lua world, however the lua world does not have enough memory to be able to store the results (it has its own smallish memory allocation units) so as a result lua_newtable(L) or other functions that can allocate memory fail and an exception is thrown. Now at this point here i would assume that it would use the longjmp function to go back to the previous restore point (created with setjmp) which could potentially be within the script that called the function, to a panic if no catch point was set. Here lies the problem, the data that was allocated on the system heap has now lost all reference to it.

I see several ways around this, firstly don't code in such a way that this can happen, ie don't use system memory in lua function where an exception call would result in the reference being lost, or something else that doesn't jump out at me in the early hours of the morning. What i would ideally like to be able to do is somehow wrap a section of lua api calls with the ability to catch the exception, and rethrow it. In order to do this i see the need to set a new exception handling point, perform the cleanup processing and then rethrow the exception. I am quite capable off adding code to lua to be able to do this, at first this looks like it would be a trivial operation, however i was wondering whether there might be an already established method for achieving this try/catch/rethrow idea?

Perhaps i have missed something totally obvious in my quest to find the answer before sending this message, if i have please point me in the direction i need to go :-)

Many Thanks,