Program crash: GC destroys an object unexpectedly

Steven Schveighoffer schveiguy at gmail.com
Thu Sep 23 17:20:18 UTC 2021


On 9/23/21 12:53 PM, eugene wrote:
> On Thursday, 23 September 2021 at 15:53:37 UTC, Steven Schveighoffer wrote:
>> Technically, they should live past the end of main, because it's still 
>> possible to receive signals then.
> 
> No, as soon as an application get SIGTERM/SIGINT,
> event queue is stopped and we do not need no
> more notifications from OS (POLLIN/POLLOUT I mean).
> 
> Stopping event queue in this case is just
> closing file descriptor obtained from epoll_create().
> After this getting POLLIN from any fd (including signal fd) is
> just impossible.
> 
> 

That's not what is triggering the segfault though. The segfault is 
triggered by the signal handler referencing the destroyed object.

So imagine the sequence:

1. ctrl-c, signal handler triggers, shutting down the loop
2. main exits
3. GC finalizes all objects, including the Stopper and it's members
4. ctrl-c happens again, but you didn't unregister the signal handler, 
so it's run again, referencing the now-deleted object.
5. segfault

It's theoretically a very very small window.

-Steve


More information about the Digitalmars-d-learn mailing list