[Issue 24596] New: Rebindable2 corrupts objects

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Jun 10 09:48:16 UTC 2024


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

          Issue ID: 24596
           Summary: Rebindable2 corrupts objects
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: critical
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: default_357-line at yahoo.de

object.destroy is, perhaps appropriately, the most destructive function ever
written.

It does something useful (resetting the value and calling the destructor) for
structs and struct derivatives, but something horrible (destroying the *class
data*) for objects. So you're always tempted to use it, but you have to
remember, every time, to disable the cases where it causes silent future
crashes.

I forgot this time.

```
module test;

import std.algorithm;

class A {
    int i;
    int getI() => i;
    this(int i) { this.i = i; }
}

void main() {
    auto arr = [new A(2), new A(3)];
    auto max = arr.maxElement!(a => a.getI);
    // Due to maxElement calling extremum which uses Rebindable2
    // which calls destroy
    // the vtable pointer of arr[0] is now zeroed out.
    assert(arr[0].getI == 2);
}
```

--


More information about the Digitalmars-d-bugs mailing list