[Issue 4624] New: std.stdio.File and std.typecons.Unique not GC-heap safe

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Aug 11 12:28:54 PDT 2010


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

           Summary: std.stdio.File and std.typecons.Unique not GC-heap
                    safe
           Product: D
           Version: D2
          Platform: Other
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody at puremagic.com
        ReportedBy: michel.fortin at michelf.com


--- Comment #0 from Michel Fortin <michel.fortin at michelf.com> 2010-08-11 15:28:53 EDT ---
Just took a look at Phobos for struct destructors. Both std.stdio.File and
std.typeconst.Unique seem unsafe to store anywhere in the GC heap (in an array
or in a class).

For std.stdio.File, it's because because the destructor assumes the
GC-allocated Impl instance can be dereferenced (which is a risky bet during the
collection that the GC will deallocate things in the "right" order):

    this(string name, in char[] stdioOpenmode = "rb")
    {
        p = new Impl(errnoEnforce(.fopen(name, stdioOpenmode),
                        "Cannot open file `"~name
                        ~"' in mode `"~stdioOpenmode.idup~"'"),
                1, name);
    }

    ~this()
    {
        if (!p) return;
        // @@@BUG@@@ These lines prematurely close the file
        //printf("Destroying file `%s' with %d refs\n", toStringz(p.name),
p.refs);
        if (p.refs == 1) close;
        else --p.refs;
    }

In struct std.typecons.Unique(T), unique calls delete on the object it
references, but since that object is in the GC heap the same problem arises: it
might already have been deallocated:

    ~this()
    {
        writeln("Unique destructor of ", (_p is null)? null: _p);
        delete _p;
        _p = null;
    }

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list