[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Why table does not have the __gc event?
- From: Ben Sunshine-Hill <sneftel@...>
- Date: Sun, 15 Jan 2006 20:37:23 -0800
On 1/15/06, CHU Run-min <churunmin@gmail.com> wrote:
> Just now, I found table does't have the __gc event, only userdata has.
> But I don't know why.
>
Basically, because it wouldn't do what you wanted it to do. Lua, like
many GCed languages, does not specify exactly when an unreferenced
object will actually be destroyed. It might be destroyed immediately;
it might be destroyed two months later. Because of this, destruction
is an unsuitable time to free scarce resources associated with an
object--closing windows, unlocking files, releasing printers, etc. If
you used __gc for this, you'd never know when a file would actually be
unlocked, so it would never be safe to try to use that file again. The
reason userdatas have __gc is so that they can free _memory_.
Bottom line: Incremental garbage collection makes finalizer methods
unsuitable for anything but freeing memory. If you need to free scarce
resources, do it manually.
Ben