GC object finalization not guaranteed

Leandro Lucarella llucax at gmail.com
Sat Apr 18 16:35:44 PDT 2009


Unknown W. Brackets, el 18 de abril a las 16:16 me escribiste:
> The simple solution is this:
> 
> 1. If your class object only involves memory, freed OS handles, etc., it
> should be used as-is.  This is most class objects.  Destructors are
> needed to clamp resource use (see File class.)

A File class implementation, for instance, can loose data if they use
buffers. For example:

class File
{
	//...

	~this()
	{
		this.flush(); // write remaining buffered data
		close(handle);
	}
}

In this case, the OS will close the handle, but the data will not be
written.

So I guess this kind of things has to go in your 2nd group.

And this is a good example for a valid use that will break if guaranteed
finalization is not provided. A log file is a good example of an object
that lasts for all the program lifetime and needs guaranteed finalization.

> 2. If your class object involves hardware handles, transactional
> assurance, or data integrity, it must be scoped.  This is the same as it
> was in C, except D has better constucts for it (see scope.)
> 
> I don't see the problem.  For the majority of objects that only involve
> memory, life is easier.  For the rest, life is still easier (just not as
> much.)

Ok, I agree. Do you see any problems with support to guaranteed
finalization?

My proposal is:
a) Add to the specs that finalization is guaranteed, in the worse case at
   program termination.
b) Standarize the gc_setTermHandlerDepth() (or whatever the name is)
c) Use a "finalize all the live data" at program exit as a default for
   the TermHandlerDepth (without running a collection, but this is
   completely implementation dependant).

Comments?

-- 
Leandro Lucarella (luca) | Blog colectivo: http://www.mazziblog.com.ar/blog/
----------------------------------------------------------------------------
GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145  104C 949E BFB6 5F5A 8D05)
----------------------------------------------------------------------------
Soy como una mosca, parada en el agua.
Y vos sos un transatlántico, querés nadar a mi lado.
Y me estás ahogando.



More information about the Digitalmars-d mailing list