Rant of the day

H. S. Teoh hsteoh at quickfur.ath.cx
Mon Jan 25 20:07:04 UTC 2021


On Mon, Jan 25, 2021 at 07:53:11PM +0000, Rumbu via Digitalmars-d wrote:
[...]
> iport std.mmfile;
> 
> struct Database
> {
>     this(string filename)
>     {
>         this.filename = filename;
>         file = new MmFile(filename, MmFile.Mode.read, 0, null);
>     }
> 
>     ~this()
>     {
>         if (file)
>         {
>             destroy(file);
>             GC.free(&file);
>         }
>     }
> }
> 
> //somewhere in code:
> {
>     Database db = Database("somefile");
>     ....
>     //here at the end of scope I want to be sure that file map is closed.
> }
[...]

Honestly, I think std.mmfile should either be refactored to be a struct
with defined lifetime (i.e., with a dtor), or the class should have a
.close method for explicitly closing the mapping.

Relying on a GC-collected object to reclaim a resource (in this case a
mmap mapping) is an anti-pattern that should be expunged from Phobos.

	https://issues.dlang.org/show_bug.cgi?id=9501


T

-- 
Meat: euphemism for dead animal. -- Flora


More information about the Digitalmars-d mailing list