Reputation: 16148
This is related to Disposing a class with an interface?
interface Bar{
}
class Foo: Bar{
int i;
this(int _i){
i = _i;
}
}
void main(){
import std.experimental.allocator.mallocator;
import std.experimental.allocator;
auto f = Mallocator.instance.make!Foo(42);
Bar b = f;
void* p = (cast(void*)b);
void* p1 = (cast(void*)f);
writeln(p);
writeln(p1);
Mallocator.instance.dispose(b);// Bad
}
Prints:
1EBE438
1EBE420
So the address of Bar
has an offset of 24 bytes. That can't be right. How do I get the correct address from an interface?
Upvotes: 2
Views: 107
Reputation: 26
From https://dlang.org/spec/abi.html#classes:
Casting a class object to an interface consists of adding the offset of the interface's corresponding vptr to the address of the base of the object. Casting an interface ptr back to the class type it came from involves getting the correct offset to subtract from it from the object.Interface entry at vtbl[0].
I don't know why this is necessary, but it seems to explain the different addresses.
Upvotes: 1