[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Calling a function on exit
- From: Tomas <tomas@...>
- Date: Wed, 9 Jun 2004 22:00:32 -0300 (BRT)
Hi Bernhard,
> I've written a little wrapper to use SDL from within Lua scripts. When I
> use SDL with C, I usually call atexit(SDL_Quit) after I initialized it.
> I have to call SDL_Quit on exit, because bad things could happen if I don't.
> However, since I initialize SDL in Lua, I have to make sure SDL_Quit
> (i.e. my wrapper function for it) gets called whenever the chunk
> execution ends. If there is an error in my script and it aborts, the
> program terminates without calling SDL_Quit. Is there some kind of
> "atexit" function for Lua? I have looked at hooks, but they don't seem
> to be made for this purpose. Unfortunately it is not an option to check
> if SDL_Quit has to be called by the interpreter after the script
> execution has ended.
I don't know SDL not even how your wrapper works, but I've made
some Lua libraries that have similar problems. A clean way to do that
is to create a userdata (it will look like a Lua object) which exports
your functions (its methods) and define a metatable for it. The __gc
field of the metatable could store a function that calls SDL_Quit.
Tomas