[Issue 17869] scope class object no longer deleted when created via factory function

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Oct 5 16:17:04 UTC 2017


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

Mathias Lang <mathias.lang at sociomantic.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mathias.lang at sociomantic.co
                   |                            |m

--- Comment #1 from Mathias Lang <mathias.lang at sociomantic.com> ---
This should have never worked to begin with!
Slightly modifying your example to expose the bug:

```
import core.stdc.stdio;

void foo()
{
    scope baseKey = Key.getKey();
}

int main(string[] argv)
{
    printf("main init\n");
    foo();
    printf("main exit\n");
    assert(Key.global !is null);
    return 0;
}

class Key
{
private:
    this() { printf("ctor\n"); }
    ~this() { printf("dtor\n"); }

    static Key global;
    static Key getKey() { return (global = new Key()); }
}
```

As you can see, in here the compiler has no way to know it *should* destroy
`baseKey` at the end of the `foo` function. And `scope` in this case is
misleading, as the `scope` reference is GC-allocated anyway.

--


More information about the Digitalmars-d-bugs mailing list