When I catch SIGINT signal in my program, how can I safely clean up resources?
In signal handler function it is impossible to call delete
operator, because I don't know how to release resource开发者_Go百科 created with new
operator.
Any ideas?
If your application is shutting down, don't worry about memory. The OS is going to throw it all away once you terminate anyway.
The things you need to clean up in your signal handler is stuff that's going to outlive your process otherwise - for example, if a child process you've created needs to exit also, you should tell it to do so.
Generally, you don't want to do much at all in your signal handler except set a boolean flag that will be reacted to by some other part of your program, typically causing some loops to fall through and allowing the familiar approach to orderly shutdown you'd use in cases not involving signals. The reason for using this flag to coordinate the shutdown is that some library functions eschew the compromises required to be safely reentered during async signal handling. Even some thread-safe functions using thread-specific memory may not cope with async reentry. A quick search found some discussion of safe functions at http://book.chinaunix.net/special/ebook/addisonWesley/APUE2/0201433079/ch10lev1sec6.html#ch10lev1sec6.
精彩评论