[Issue 1250] std.stream.BufferedFile should have a destructor with close()

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon May 28 03:19:13 PDT 2007


http://d.puremagic.com/issues/show_bug.cgi?id=1250


fvbommel at wxs.nl changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         OS/Version|Windows                     |All
         Resolution|                            |INVALID




------- Comment #1 from fvbommel at wxs.nl  2007-05-28 05:19 -------
You can't do that; it causes undefined behavior if BufferedFile is collected by
the GC. BufferedFile internally uses a File, and GC-collected objects can't
safely reference other GC-able objects in their destructors since they may have
been collected earlier and accessing deleted objects is Bad(TM).
File can call close() in the destructor because it doesn't need to access
GC-able objects to close(), it just performs the needed OS calls directly.

The only way to safely put a close() in the destructor of BufferedFile (or
BufferedStream, its superclass) would be to make it a "scope" class to require
deterministic destruction, but that would disallow a lot of use cases.

You can add a "scope(exit) f.close();" after the declaration of f to safely get
the closing behavior you want.


-- 



More information about the Digitalmars-d-bugs mailing list