gpt4 book ai didi

.net - 如何处理 System.AccessViolationException?

转载 作者:行者123 更新时间:2023-12-01 02:37:43 26 4
gpt4 key购买 nike

我在我的程序中使用了一个外部 C 库,用 C++/CLI 和 .NET 框架 4 编码。有时库崩溃,我收到消息:

Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory.



所以我尝试用 try/catch 块来处理它:
try
{
Init(); //< lib call which sometimes crashes
}
catch (Exception^ e)
{
// handle the error
}

但异常仍未被捕获:我的程序在进入 catch 块之前就崩溃了。

如何处理此异常以防止我的程序崩溃?

最佳答案

为清楚起见,我复制/粘贴 the answer在评论中给出 Flot2011这里:

There are a few ways to get around this:

  • Recompile as a .NET 3.5 assembly and run it in .NET 4.0.
  • Add a line to your application's config file under the configuration/runtime element: <legacyCorruptedStateExceptionsPolicy enabled="true|false"/>
  • Decorate the methods you want to catch these exceptions in with the HandleProcessCorruptedStateExceptions attribute. See this article for details.

关于.net - 如何处理 System.AccessViolationException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9499897/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com