www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 24596] New: Rebindable2 corrupts objects

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 puremagic.com
          Reporter: default_357-line 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);
}
```

--
Jun 10