[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: destructors
- From: Glenn Maynard <glenn@...>
- Date: Sat, 23 Sep 2006 02:59:45 -0400
Lua finalizers can't be used for many of the things C++ dtors are used
for. When closing a file, you often can't wait until GC feels like
cleaning up the file for the file to be closed (it'll sit on locks,
often preventing other processes from writing to the file). Lock objects
("lock this mutex for the lifetime of this object") can't be done, since
the mutex will remain locked for a while. TCP/IP streams will wait to
close the stream, and TCP listeners will keep listening on the port.
Of course, you can explicitly close files and unlock mutexes, but that's
inconvenient in Lua; having to carefully, explicitly "close" objects
goes badly against the stlye of Lua, where I should just be able to clear
my reference when I'm done with something. And the problem propagates
upward; if I have a Lua class to load a PNG incrementally (p:Open("foo.png");
p:ReadRow()), and I want the file closed as soon as it's done with it,
that class now has to have a "close" method, too. I'd also have to
force a complete GC in every error handler.
I think Lua's GC scheme isn't capable of actually implementing dtors
efficiently, but how do people deal with this? I can't just force a
complete GC cycle; aside from performance, that's just the "close"
problem in another form.
--
Glenn Maynard