ctrl+c and destructors

PauloPinto pjmlp at progtools.org
Sun Sep 29 23:15:21 PDT 2013


On Monday, 30 September 2013 at 02:13:47 UTC, H. S. Teoh wrote:
> 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

Windows uses kernel level exceptions for some signals, known as 
Structured Exception Handling. For others, callback handlers are 
used instead, similar to signal handlers.

http://msdn.microsoft.com/library/windows/desktop/ms680657.aspx

signal() is available,

http://msdn.microsoft.com/en-us/library/xdkz3x12%28v=vs.110%29.aspx

However, since I never used signal on Windows, here is the 
specific Windows API for the same purpose,

http://msdn.microsoft.com/en-us/library/ms686016%28VS.85%29.aspx

--
Paulo


More information about the Digitalmars-d mailing list