[Issue 21442] New: Calling AA.remove from a destructor might lead to InvalidMemoryOperationError

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Dec 1 09:58:02 UTC 2020


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

          Issue ID: 21442
           Summary: Calling AA.remove from a destructor might lead to
                    InvalidMemoryOperationError
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: critical
          Priority: P1
         Component: druntime
          Assignee: nobody at puremagic.com
          Reporter: pro.mathias.lang at gmail.com

Calling `AA.remove` might shrink the AA, and shrinking an AA allocates.
Hence, calling `AA.remove` from a GC-run destructor is not possible as it could
lead to an `InvalidMemoryError` being thrown. Example:

```
import core.memory;

size_t[size_t] glob;

class Foo
{
    size_t count;

    this (size_t entries) @safe
    {
        this.count = entries;
        foreach (idx; 0 .. entries)
            glob[idx] = idx;
    }

    ~this () @safe
    {
        foreach (idx; 0 .. this.count)
            glob.remove(idx);
    }
}

void main ()
{
    bar();
    GC.collect(); // Needs to happen from a GC collection
}

void bar () @safe
{
    Foo f = new Foo(16);
}
```

This triggers:
```
core.exception.InvalidMemoryOperationError at src/core/exception.d(647): Invalid
memory operation
----------------
```

It's absolutely ridiculous that one can't call `AA.remove` in a destructor
(provided the AA is known to be reachable by other means). For reference,
vibe.d (eventcore actually) does this.

--


More information about the Digitalmars-d-bugs mailing list