我正在为一个非托管C++项目编写一个使用C++/CLI的包装库。目标是将此库展示给C#应用程序。这是迄今为止我所拥有的。从Finalize中删除一个非托管对象引发AccessViolationException
#pragma once
#include "Settings.h"
#include "Settings/SettingsPrivate.h"
public ref class Settings
{
public:
Settings();
virtual ~Settings();
protected:
!Settings();
public:
unsigned char GetModel(int iNumber);
private:
CSettings* m_pSettings;
};
#include "stdafx.h"
#include "Managed/Settings.h"
Settings::Settings()
{
// Pointer to unmanaged object
m_pSettings = new CSettings();
}
Settings::~Settings()
{
this->!Settings();
}
Settings::!Settings()
{
if (m_pSettings)
{
delete m_pSettings;
m_pSettings = NULL;
}
}
unsigned char Settings::GetModel(int iNumber)
{
return m_pSettingss->GetModel(iNumber);
}
的代码执行测试应用程序,我写的罚款。函数调用成功。问题是当GC完成这个对象时,它会抛出一个异常。
型“System.AccessViolationException”未处理的异常发生在Wrapper.dll
其他信息:试图读取或写入保护内存。这通常表明其他内存已损坏。
我看不出任何明显的原因,为什么抛出这个异常。我试图通过从我的C#应用程序中调用Dispose来显式地处理对象。它仍会抛出相同的异常。
这里的测试应用程序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WrapperTest
{
class Program
{
static void Main(string[] args)
{
Settings settings = new Settings();
byte b = settings.GetModel(0);
settings.Dispose();
return;
}
}
}
会有人指出我在做什么错?
没有repro,看不到问题。我认为问题出现在SettingsPrivate,我猜可能会造成堆损坏。 – 2011-04-01 01:06:04
如果您在调试器中启用了第一次机会AV(“调试 - >异常 - > Win32异常 - >访问冲突”;您可能想在“工具 - >选项 - >调试”中禁用“只是我的代码”),什么被访问(你还需要在C#项目中启用本地调试,因为我假设你正在启动它)?没有什么是跳出来的,因为这段代码错了。 – 2011-04-01 02:52:34
@Hans:昨晚你的评论让我走上正确的轨道来解决我的问题。如果你发布了答案,我会给你这个问题的功劳。 – Nathanael 2011-04-01 15:51:29