我在我的.net应用程序中使用COM对象(MODI).我调用的方法抛出一个System.AccessViolationException,它被Visual Studio拦截.奇怪的是我在try catch中包含了我的调用,它包含AccessViolationException,COMException和其他所有东西的处理程序,但是当Visual Studio(2010)拦截AccessViolationException时,调试器会中断方法调用(doc.OCR),如果我单步执行,它将继续到下一行,而不是进入catch块.另外,如果我在visual studio外部运行,我的应用程序崩溃了.如何处理COM对象中引发的此异常?
MODI.Document doc = new MODI.Document();
try
{
doc.Create(sFileName);
try
{
doc.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, false, false);
sText = doc.Images[0].Layout.Text;
}
catch (System.AccessViolationException ex)
{
//MODI seems to get access violations for some reason, but is still able to return the OCR text.
sText = doc.Images[0].Layout.Text;
}
catch (System.Runtime.InteropServices.COMException ex)
{
//if no text exists, the engine throws an exception.
sText = "";
}
catch
{
sText = "";
}
if (sText != null)
{
sText = sText.Trim();
} …Run Code Online (Sandbox Code Playgroud) 一些背景故事:
我搜索了stackoverflow和Google来找出这个特殊异常的起源.
我们有一个Windows窗体(C#,. NET 2.0)应用程序偶尔会抛出一个System.AccessViolationException(下面的堆栈跟踪).Windows XP(SP3)和Windows Vista(SP2)都发生了异常.从用户输入,我们已经确定当更改ComboBox的选定索引时发生异常.
ComboBox是标准的Windows窗体组件,它包含在继承的自定义控件中System.Windows.Forms.UserControl.
我的主要问题是:
这个异常来自哪里?
SelectedIndexChanged导致它的事件调度的代码吗?有关stackoverflow的相关主题:
堆栈跟踪:
Message: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Type: System.AccessViolationException
at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
at System.Windows.Forms.Control.DefWndProc(Message& m)
at System.Windows.Forms.Control.WmCommand(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ComboBox.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 …Run Code Online (Sandbox Code Playgroud) 我有一个C++客户端到C++/CLI DLL,它初始化一系列C#dll.
这曾经工作过.失败的代码没有改变.在抛出异常之前,不会调用已更改的代码.我的编译环境已经改变,但是在具有类似于旧环境的机器上重新编译仍然失败.(编辑:我们在答案中看到这并不完全正确,我只是在旧环境中重新编译库,而不是库和客户端一起重新编译.客户端项目已经升级,无法轻易返回.)
除了我之外,有人重新编译了库,我们开始遇到内存管理问题. The pointer passed in as a String must not be in the bottom 64K of the process's address space. 我重新编译了它,并且没有代码更改都运行良好.(警报#1)最近它被重新编译,并且字符串的内存管理问题重新出现,而这次它们并没有消失.新的错误是Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
我很确定问题不在我看到异常的位置,代码在成功和失败的构建之间没有变化,但我们应该检查完成.忽略事物的名称,我对这些字符串的设计没有多少控制权.抱歉混淆,但请注意_bridge并且bridge是不同的事情.由于这个问题已经太长,所以缺少大量代码.
在库中定义:
struct Config
{
std::string aye;
std::string bee;
std::string sea;
};
extern "C" __declspec(dllexport) BridgeBase_I* __stdcall Bridge_GetConfiguredDefaultsImplementationPointer(
const std::vector<Config> & newConfigs, /**< new configurations to apply **/
std::string configFolderPath, /**< folder …Run Code Online (Sandbox Code Playgroud)