Debug.Assert 导致我的应用程序退出
我创建了一个包含许多实用函数的库来执行一些任务,并将其放置在它自己的 DLL 中。
在这个库中的所有方法中,我放置了 Debug.Assert 语句来验证应用程序的状态。
问题是,当 Assert 触发时(条件为 false),它会显示对话框,单击它的“中止”会导致使用此 DLL 的整个应用程序崩溃。
这提出了两个问题:
这可以避免吗?我希望这会抛出异常而不退出应用程序。
DLL 为什么会导致加载它的应用程序退出?这不是一个安全漏洞吗?
谢谢
I created a library with many utility functions to perform some tasks and placed it in it's own DLL.
Throughout the methods in this library, i've placed Debug.Assert statements to verify the state of the application.
The problem is, when the Assert is firing (the condition is false), it presents the dialog, clicking "Abort" on it causes the entire application that uses this DLL to crash.
This poses 2 questions:
Can this be avoided? i'd expect this to throw an exception without exiting the application.
How come a DLL can cause the application that loads it to exit? isn't this a security breach?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Debug.Assert 并不打算出现在发布代码中(因此是“Debug”:))。选择 Abort 意味着终止引发断言的进程。如果您选择“忽略”,它应该继续,并且无论哪种方式,这都不会在发布版本中发生,因为 Debug.Assert 将被删除。
编辑:这是 MSDN 解释的链接: http://msdn.microsoft.com /en-us/library/e63efys0.aspx
The Debug.Assert is not intended to end up in release code (hence 'Debug' :) ). Choosing Abort means kill the process that raised the assert. If you choose Ignore, it should carry on, and either way this should not happen in the Release build as the Debug.Assert will be stripped out.
Edit: Here's a link to the MSDN explanation: http://msdn.microsoft.com/en-us/library/e63efys0.aspx
如果你想抛出异常,为什么不直接抛出异常呢?
更改
为
断言将离开应用程序,断言在这里是为了保护您免受编码器错误的影响。如果一个函数永远不应该接收空指针,并且如果接收到的话那就是一个错误,那么断言将是正确的工具。
如果你想表达一种例外情况,那么例外就是你正在寻找的工具
If you want to have an exception thrown, why dont you just throw an exception?
Change
to
Assert will leave the application, assertion are here to protect you from coder's mistake. If a function should never receive a null pointer, and if it does then it would be a mistake, then an assert would be the right tool.
If you want to express an exceptionnal situation, than the exception is the tool you are looking for