如何保证在Windows C ++中清除代码运行(SIGINT,bad alloc和closed window) [英] How do I guarantee cleanup code runs in Windows C++ (SIGINT, bad alloc, and closed window)

查看:187
本文介绍了如何保证在Windows C ++中清除代码运行(SIGINT,bad alloc和closed window)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows C ++控制台程序,如果我在程序结束时不调用 ReleaseDriver(),一些硬件进入坏状态,无法重新启动时无法使用。
我想确保 ReleaseDriver()即使程序退出异常也会运行,例如,如果我点击 Ctrl + C 或关闭控制台窗口。

I have a Windows C++ console program, and if I don't call ReleaseDriver() at the end of my program, some pieces of hardware enter a bad state and can't be used again without rebooting. I'd like to make sure ReleaseDriver() gets runs even if the program exits abnormally, for example if I hit Ctrl+C or close the console window.

我可以使用 signal()信号处理程序 SIGINT 。这工作正常,虽然作为程序结束它弹出一个恼人的错误未处理的Win32异常发生...。

I can use signal() to create a signal handler for SIGINT. This works fine, although as the program ends it pops up an annoying error "An unhandled Win32 exception occurred...".

我不知道如何处理控制台窗口被关闭的情况,(更重要的是)我不知道如何处理由坏内存引起的异常访问等。

I don't know how to handle the case of the console window being closed, and (more importantly) I don't know how to handle exceptions caused by bad memory accesses etc.

感谢您的帮助!

推荐答案

,您可以通过调用 SetUnhandledExceptionFilter()来创建未处理的例外过滤器。 。一旦完成,任何时候生成的异常不会在你的应用程序中的某个地方处理,你的处理程序将被调用。

Under Windows, you can create an unhandled exception filter by calling SetUnhandledExceptionFilter(). Once done, any time an exception is generated that is not handled somewhere in your application, your handler will be called.

你的处理程序可以用来释放资源,文件(请参阅 MiniDumpWriteDump )或任何您需要确保完成。

Your handler can be used to release resources, generate dump files (see MiniDumpWriteDump), or whatever you need to make sure gets done.

请注意,有很多'gotchas'围绕你如何写你的异常处理函数。特别是:

Note that there are many 'gotchas' surrounding how you write your exception handler function. In particular:


  1. 您不能调用任何CRT函数,例如 new li>
  2. 您无法执行任何基于堆栈的分配

  3. 如果您在处理程序中执行任何导致异常的操作,Windows将立即通过翻转骨骼出来的背。

  1. You cannot call any CRT function, such as new
  2. You cannot perform any stack-based allocation
  3. If you do anything in your handler which causes an exception, Windows will immediately terminate your application by ripping the bones out of its back. You get no further chances to shut down gracefully.

您可以调用许多Windows API函数。但是你不能 sprintf delete 。 ..总之,如果它不是一个WINAPI函数,它可能是不安全的。

You can call many Windows API functions. But you can't sprintf, new, delete... In short, if it isn't a WINAPI function, it probably isn't safe.

由于上述所有,建议使所有您的处理函数 static 中的变量。您将无法使用sprintf,因此您必须提前格式化字符串,在初始化期间。只要记住,当你的处理程序被调用时,机器处于非常不稳定的状态。

Because of all of the above, it is advisable to make all the variables in your handler function static variables. You won't be able to use sprintf, so you will have to format strings ahead of time, during initialization. Just remember that the machine is in a very unstable state when your handler is called.

这篇关于如何保证在Windows C ++中清除代码运行(SIGINT,bad alloc和closed window)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆