Program crash: GC destroys an object unexpectedly

jfondren julian.fondren at gmail.com
Mon Sep 13 18:45:22 UTC 2021


On Monday, 13 September 2021 at 17:18:30 UTC, eugene wrote:
> Then after pressing ^C (SIGINT) the program gets SIGSEGV, since 
> references to sg0 and sg1 are no longer valid (they are 
> "sitting" in epoll_event structure).

engine/ecap.d(54): Error: field `EpollEvent.es` cannot assign to 
misaligned pointers in `@safe` code
engine/ecap.d(56): Error: cannot take address of local `e` in 
`@safe` function `registerEventSource`

from adding @safe to ecap.EventQueue.registerEventSource, and 
then from using a @trusted block to silence the first complaint.

Instead of using a temporary EpollEvent array in EventQueue.wait, 
you could make the array an instance variable and have 
registerEventSource populate it directly, so that the GC can 
always trace from this array to an EnventSource.

... however, I don't think this fixes your problem, or is your 
only problem, since the segfault's still observed when this 
memory is leaked:

```d
     void registerEventSource(EventSource es) {
         import core.memory : pureMalloc, GC;

         auto p = cast(EpollEvent*) pureMalloc(EpollEvent.sizeof);
         p.event_mask = 0;
         p.es = es;
         GC.addRoot(p);
         int r = epoll_ctl(id, EPOLL_CTL_ADD, es.id, p);
         assert(r == 0, "epoll_ctl(ADD) failed");
     }
```


More information about the Digitalmars-d-learn mailing list