Program crash: GC destroys an object unexpectedly

jfondren julian.fondren at gmail.com
Tue Sep 14 16:07:00 UTC 2021


On Tuesday, 14 September 2021 at 15:37:27 UTC, eugene wrote:
> On Tuesday, 14 September 2021 at 14:56:00 UTC, jfondren wrote:
>> You could fix this by having a 128-bit struct and passing C an 
>> index into it
>
> It is another "not so funny joke", isn't it?

No. And when was the first one?

> ```d
> align (1) struct EpollEvent {
>     align(1):
>     uint event_mask;
>     EventSource es;
>     /* just do not want to use that union, epoll_data_t */
> }
> static assert(EpollEvent.sizeof == 12);
> ```

That's 96 bits. Add 32.

```d
class EventSource { }
align(1) struct EpollEvent {
     align(1):
     uint event_mask;
     EventSource es;
}
struct OuterEpollEvent {
     int _dummy;
     uint event_mask;
     EventSource es;
}
EpollEvent* epollEvent(return ref OuterEpollEvent ev) @trusted {
     return cast(EpollEvent*) &ev.event_mask;
}
void dumpEpollEvent(EpollEvent* ev) @trusted {
     import std.stdio : writeln;

     writeln(*ev);
}

unittest {
     // can't be @safe:
     // Error: field `EpollEvent.es` cannot modify misaligned 
pointers in `@safe` code
     EpollEvent ev;
     ev.es = new EventSource; // misaligned
}

@safe unittest { // this is fine
     OuterEpollEvent ev;
     ev.event_mask = 0;
     ev.es = new EventSource; // not misaligned
     ev.epollEvent.dumpEpollEvent;
}
```


More information about the Digitalmars-d-learn mailing list