我在我的.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) 经过对valgrind的大量调查后,我得出结论,std :: vector制作了你想要push_back的对象的副本.
这是真的吗?没有副本,向量不能保留对象的引用或指针?
谢谢
你可以将一个结构的一个实例分配给另一个,如下所示:
struct Test t1;
struct Test t2;
t2 = t1;
Run Code Online (Sandbox Code Playgroud)
我看到它适用于简单的结构,它是否适用于复杂的结构?
编译器如何知道如何根据类型复制数据项,即区分int和字符串?
例
int *ptr;
*ptr = 1000;
Run Code Online (Sandbox Code Playgroud)
我可以使用标准C++捕获内存访问冲突异常,而无需使用任何特定的Microsoft.
我们的应用程序经历了奇怪的致命System.AccessViolationException.我们看到这些因为我们已经配置了AppDomain.CurrentDomain.UnhandledException事件来记录异常.
Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at Bootstrap.Run() in e:\build-dir\src\Bootstrap.cs:line 25
Run Code Online (Sandbox Code Playgroud)
异常本身似乎不包含比"尝试读取或写入受保护的内存"消息更多的信息.这通常表明其他内存已损坏.
UPDATE
好的,所以我在调试时遇到了很多问题.我正在使用VS2013 Pro和Windows 8.1.两者都是最新的.问题是,当我开始调试时,它会抛出此错误的一半时间:
System.Windows.Forms.dll中发生了未处理的"System.AccessViolationException"类型异常
附加信息:尝试读取或写入受保护的内存.这通常表明其他内存已损坏.
它也不是我的代码的错.我做了一个简单的测试作为例子如下.请注意,我没有从此应用程序引用System.Windows.Forms.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
List<int> testing = new List<int>();
for(int i =0; i < 50; i++)
{
testing.Add(i);
}
for (int i = 0; i < 50; i++)
{
Console.WriteLine(testing[i].ToString());
}
Console.ReadLine();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我不知道是什么造成了这个.如果我单击"确定"并再次运行它,它将起作用.偶尔我必须做两次.
有任何想法吗?
堆栈跟踪:

假设我有一个包含std :: string的结构,如下所示:
struct userdata{
int uid;
std::string username;
}
Run Code Online (Sandbox Code Playgroud)
我是否需要创建一个复制ctor或任何东西来从函数返回它或在STL容器中使用它?考虑这个功能:
userdata SomeClass::GetUserData(unsigned int uid)
{
//do error checking and other stuff...
//m_usermap is std::map<unsigned int, userdata>
return m_usermap[uid];
}
Run Code Online (Sandbox Code Playgroud)
当我将用户数据结构插入到std :: map中时,会创建一个struct的副本,对吧?是否使用username字段的值创建了新的std :: string,或者是否发生了某种按位复制(这会很糟糕)?类似地,当我从GetUserData方法返回userdata结构时,它是否有一个包含用户名的独立字符串,还是我需要定义一个copy ctor并显式创建一个新字符串?
我正在与VS2010和VS2012的项目合作.
VS2010项目调用VS2012中的函数,反之亦然.这开始工作正常,但是当我还需要在两个项目之间共享变量时,我注意到变量似乎没有相同的内存对齐,并且每个项目都以不同的方式解释相同的内存地址.
更新:它似乎只发生在使用STL容器,其他结构和类不包含std ::工作正常.
为了说明问题,以下代码在不同的Visual Studio版本上运行时应该得到不同的结果.
#include <string>
#include <vector>
int main()
{
int stringSize = sizeof(std::string); // Yelds 32 on VS2010, 28 on VS2012
int intVectorSize = sizeof(std::vector<int>); // Yelds 20 on VS2010, 16 on VS2012
return 0;
};
Run Code Online (Sandbox Code Playgroud)
我不能将这两个项目更新到同一个版本,因为我有一些与每个版本相关的依赖项.
有没有人知道解决方案或绕过问题的方法?
我会尽快将这两个项目升级到VS2012编译器,但是现在我正在寻找一个快速而肮脏的解决方案,所以我可以相处工作.由于它似乎只发生在STL容器中,是否可以在所有项目中使用旧版本的库?或者是否有可能欺骗编译器?也许改变填充大小?
此外,std :: vector中的第一个元素看起来很好,只有向量中的后续元素似乎被扰乱.(见图.)

在2010年和2012年编译的"main.cpp"中调试"Fetched"变量的图像.
有人希望我澄清变量的共享方式.
我们在VS2012编译模式下将第一个项目编译成DLL,然后尝试在VS2010中访问该项目.
这是重新创建问题的一些代码.如果您想亲自尝试,可以在此处下载完整的VS2012解决方案.
使用VS2012将此代码编译为DLL.
DllExport.h
#ifdef DLLHELL_EX
#define DLL_API __declspec(dllexport)
#else
#define DLL_API __declspec(dllimport)
#endif
#include …Run Code Online (Sandbox Code Playgroud)