我有一个使用一些非托管 GDI+ 功能的 C# 应用程序,我得到一个(非托管?)异常:
Log Name: Application
Source: .NET Runtime
Date: 7/1/2013 9:14:58 AM
Event ID: 1026
Task Category: None
Level: Error
Keywords: Classic
User: N/A
Computer: sth
Description:
Application: sth
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.AccessViolationException
堆栈跟踪是:
at System.Drawing.SafeNativeMethods+Gdip.IntGdipDisposeImage(System.Runtime.InteropServices.HandleRef)
at System.Drawing.SafeNativeMethods+Gdip.IntGdipDisposeImage(System.Runtime.InteropServices.HandleRef)
at System.Drawing.Image.Dispose(Boolean)
at System.Drawing.Image.Dispose()
at “..()
at Aspose.Cells.Charts.Chart.ToImage(System.IO.Stream, Aspose.Cells.Rendering.ImageOrPrintOptions)
我可以防止它完全崩溃吗?我曾尝试使用不带参数的 catch 语句,但 sth 告诉我这还不够。我用这些错误的代码制作了自己的 C++ DLL,但似乎没有任何效果,除非 DLL 抛出某事:
__declspec(dllexport) void __cdecl Function1(void) // This doesn't get caught.
{
char *chrs = new char[1000];
delete []chrs;
delete []chrs;
}
__declspec(dllexport) void __cdecl Function3(void) // This doesn't get caught.
{
try
{
MessageBox(NULL, L"AAA", L"Caption", MB_OK);
char *chrs = NULL;
chrs[0] = 'a';
}
catch(...)
{
MessageBox(NULL, L"BBB", L"Caption", MB_OK); // I get no messagebox here... (probably it doesn't get caught)
}
}
__declspec(dllexport) void __cdecl Function2(void) // This gets caught as a .NET Exception.
{
throw "Oh My!";
}
那么,我可以做些什么来防止我的EXE崩溃吗?
编辑
另外,这是否System.AccessViolationException
意味着这是一个托管异常?如果,是的,我想知道为什么我没有抓住它...我的代码包含在 try{}catch(){} 中...
编辑2
我的猜测是 GDI+ 破坏了堆,CLR 稍后检测到它并抛出异常。但它不能被捕获,因为它不是从 try 块内抛出的。在这种情况下我可以做某事吗?