ctrl+c and destructors

H. S. Teoh hsteoh at quickfur.ath.cx
Sun Sep 29 19:12:22 PDT 2013


On Mon, Sep 30, 2013 at 03:48:07AM +0200, Adam D. Ruppe wrote:
> Is there anything we can do to automatically clean up if the user
> hits ctrl+c on Linux?
> 
> I just had my system get messed up because I was allocating shared
> memory with the X server, which was released in the destructor...
> but that never got called because I killed the program with ctrl+c.
> Then the system ran out of shm handles and I had to clean that up
> before i could start a bunch of programs again.
> 
> Of course, a possible solution is to set up a signal handler in my
> own program, but even with that, tracking all the dtors that need to
> actually be called sounds difficult, especially as the program gets
> more involved.
[...]

Well, ctrl-C can be handled, so the way I'd do it is to set up a signal
handler for SIGINT and have it write something to a self-pipe read by
the event handler, then the event handler can throw an Exception (which
should cause dtors to run as the stack unwinds).

I've no idea how signals work in Windows so I don't know if it's even
possible to have a consistent D implementation of signal-handling.


T

-- 
Change is inevitable, except from a vending machine.


More information about the Digitalmars-d mailing list