我有一个用 C++/CLI 编写的自定义 .Net 接口:
public interface class IBackgroundExtractor
{
};
在我的 C# 应用程序中,该接口由以下人员使用:
private IBackgroundExtractor extractor = null;
上面的代码在我的电脑(安装了 Visual Studio)中运行顺利,但在另一台电脑上崩溃了(没有安装 Visual Studio):
AppName: speedtest.exe AppVer: 1.0.0.0 ModName: kernel32.dll
ModVer: 5.1.2600.5512 Offset: 00012aeb
如果我删除空分配,代码将在两台计算机上运行:
private IBackgroundExtractor extractor;
但是,我用纯 C# 制作了另一个接口。将接口设置为 null 不会使程序崩溃:
interface IAnotherInterface
{
}
private IAnotherInterface test = null;
我的 C++/CLI 界面有什么问题?
[评论]
我创建了两个“干净”的新项目进行测试,第一个是 C++/CLI 类库(新项目 -> Visual C++ -> CLR -> 类库)。将以下行添加到库的 .h 文件中:
public interface class ITest {
};
然后创建一个 Windows Form Application 项目(Net Project -> Visual C# -> Windows -> Windows Forms Application)并在主函数中声明一个 ITest 变量:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
ITest xxx = null;
}
该程序将在开发计算机(安装了 Visual Studio)中运行,但在另外两台计算机中崩溃。其中一台崩溃的计算机是物理机,另一台是虚拟机。
我正在使用 Windows XP Pro SP3、Visual Studio 2010 和 .Net Framework 4.0。