GC object finalization not guaranteed

Fawzi Mohamed fmohamed at mac.com
Sat Apr 18 13:48:40 PDT 2009


On 2009-04-18 22:25:32 +0200, Don <nospam at nospam.com> said:

> Leandro Lucarella wrote:
>> Robert Jacques, el 18 de abril a las 11:56 me escribiste:
>>> On Sat, 18 Apr 2009 11:24:14 -0400, Leandro Lucarella <llucax at gmail.com> wrote:
>>>> I've just found out[1] this[2]:
>>>> 
>>>> 	The garbage collector is not guaranteed to run the destructor for
>>>> 	all unreferenced objects.
>>>> 
>>>> Is there any reason why D can't guarantee that all finalizers will be
>>>> called, at least when the program ends?

when the main thread ends other threads might be running, and removing 
memory they are using is not necessarily a good idea.
Exceptions might delay things indefinitely and allocate more memory.

Actually with tango you can tweak what will happen with 
gc_getTermCleanupLevel (in gc.d), but I don't think that there is a 
satisfying solution for everybody. I think that the default in tango is 
quite reasonable (normal collect).

>>> Well, a couple of quick tests show that under normal situations (i.e.
>>> normal program termination and termination from an exception) the
>>> finalizers do run (D2). However, if a finalizer throws an exception,
>>> then the rest of the finalizers aren't called. Also, if you call
>>> std.c.stdlib.exit, the finalizers won't run.
>> 
>> Well, I'm not talking about experimentation, I'm talking about source code
>> ;)
>> 
>> The current GC implementation don't call finalizers for data that's still
>> reference when the program *ended* (this is allowed by the specs, so it's
>> fine, the question is why it's allowed by the specs).
> 
> I don't understand why D even has finalizers. Can they do anything useful?

Two things that I acn think of, there might be more:
- Release external resources. Well probably it is better not to rely on 
them if the resource is finite (basically always), but it can be useful 
at times
- weak refs




More information about the Digitalmars-d mailing list