Skip to content

[REG 2.096] Double destruction of new'ed aggregates since -preview=dtorfields became the default #18037

Open

Description

@dlangBugzillaToGithub

Mathias LANG (@Geod24) reported this on 2021-06-02T11:53:51Z

Transferred from https://issues.dlang.org/show_bug.cgi?id=21989

CC List

  • Mai Lapyst

Description

```D
import std;

pragma(msg, __VERSION__);

class Test {
    S s;
    
    this()
    {
        if (uniform(1, 10) != 0)
        	throw new Exception("oops");
        s = S(13);
    }
}

struct S {
    int x = 42;
    int y;
    this(int y) { this.y = y; };
    this(this) { assert(x == 42); }
    ~this()
    {
        if (x != 42) writeln("OH NO!");
        x = 0; // omitting this makes "OH NO!" go away
    }
}

void main()
{
    try new Test;
    catch (Exception e) writeln("Expected fail: ", e.msg);
}
```

Result:
```
2096L
Expected fail: oops
OH NO!
```

This happens because the class ctor destructs it, then the GC, upon collecting the object, finalize the class again.

Activity

  1. dlangBugzillaToGithub commented on Jun 2, 2021

    @dlangBugzillaToGithub
    Author

    pro.mathias.lang (@Geod24) commented on 2021-06-02T11:54:25Z

    Bug found in a large application using Vibe.d: https://github.com/vibe-d/vibe-core/issues/283
  2. dlangBugzillaToGithub commented on Jun 2, 2021

    @dlangBugzillaToGithub
    Author

    moonlightsentinel commented on 2021-06-02T12:40:42Z

    Thats bad. I guess the destructed class instance should be GC.free'd if the ctor is aborted by an exception. Alternatively could also reset the instance to it's init value.
  3. dlangBugzillaToGithub commented on Jun 6, 2021

    @dlangBugzillaToGithub
    Author

    moonlightsentinel commented on 2021-06-06T11:30:07Z

    Not restricted to classes, also affects GC-allocated structs
  4. dlangBugzillaToGithub commented on Jun 6, 2021

    @dlangBugzillaToGithub
    Author

    dlang-bot commented on 2021-06-06T15:09:57Z

    @MoonlightSentinel created dlang/dmd pull request #12641 "Fix 21989 - Reset destructed instance with preview=dtorfields" fixing this issue:
    
    - Fix 21989 - Reset destructed instance with preview=dtorfields
      
      Emplacing the initializer ensures that the GC won't run the destructor
      on an invalid instance.
      
      Preventing the destructor call from the GC doesn't seem to be feasible
      due to the potentually lesser qualified GC interface (not `pure`, ...).
    
    https://github.com/dlang/dmd/pull/12641
  5. dlangBugzillaToGithub commented on Nov 11, 2023

    @dlangBugzillaToGithub
    Author

    info commented on 2023-11-11T06:46:46Z

    It seems that in 2.103.0 this bug was fixed; atleast the provided example doesn't produces the "OH NO!" anymore:
    
    ```
    2103L
    Expected fail: oops
    ```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P1Severity:Regression

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions