Program crash: GC destroys an object unexpectedly

eugene dee0xeed at gmail.com
Sat Sep 18 09:39:24 UTC 2021


On Tuesday, 14 September 2021 at 20:59:14 UTC, Ali Çehreli wrote:
> On 9/14/21 9:56 AM, eugene wrote:
>
> > On Tuesday, 14 September 2021 at 16:43:50 UTC, jfondren wrote:
>
> >> The misaligned pointer and the
> >> reference-containing struct that vanishes on the return of
> your
> >> corresponding function are both problems for this.
> >
> > where did you find 'misaligned pointer'?...
>
> I think it's the align(1) for EpollEvent.

The definition of this struct was taken from
/usr/include/dmd/druntime/import/core/sys/linux/epoll.d

```d
version (X86_Any)
{
     align(1) struct epoll_event
     {
     align(1):
         uint events;
         epoll_data_t data;
     }
}
```

I am using my own definition, because data field
has not any special meaning for the Linux kernel,
it is returned as is by epoll_wait().
I am always using this field as pointer to EventSource.

This struct has to be 12 bytes for x86 arch,
in /usr/include/linux/eventpoll.h it looks like this:

```c
struct epoll_event {
         __u32 events;
         __u64 data;
} EPOLL_PACKED;
```

At some moment I had different definition (align is only inside):

```d
struct EpollEvent {
     align(1):
     uint event_mask;
     EventSource es;
     /* just do not want to use that union, epoll_data_t */
}
```
But it's appeared:

1) relatively fresh gdc (from Linux Mint 19) does the right 
thing, the structure is packed and has 12 bytes size.
2) old gdc (from Debian 8) produces 16 bytes EventEpoll and both 
programs
gets SIGSEGV right after first return from epoll_wait(), hence 
this check:

```d
static assert(EpollEvent.sizeof == 12);
```

If the reason for crash was in EpollEvent alignment,
programs would segfaults always very soon after start,
just right after the very first return from epoll_wait().








More information about the Digitalmars-d-learn mailing list