开发者

Setting custom interface written in C++/CLI to null may causes program to crash

I have a custom .Net interface written in C++/CLI:

public interface class IBackgroundExtractor
{
};

In my C# application, the interface is used by:

private IBackgroundExtractor extractor = null;

The above code runs smoothly in my computer (which installed visual studio) but it crashed in another one (without installing visual studio):

AppName: speedtest.exe   AppVer: 1.0.0.0     ModName: kernel32.dll
ModVer: 5.1.2600.5512    Offset: 00012aeb

If I remove the null assignment, the code will run in both computer:

private IBackgroundExtractor extractor;

However, I made another interface in pure C#. Setting the interface to null will not make the program to crash:

interface IAnotherInterface
{
}
private IAnotherInterface test = null;

What's wrong in my C++/CLI interface?

[Remarks]

I've create two 'clean' new projects for testing, the first one is a C++/CLI Class Library (New Project -> Visual C++ -> CLR -> Class Library). Add the following lines into the .h file of the libr开发者_开发问答ary:

public interface class ITest {
};

Then create a Windows Form Application project (Net Project -> Visual C# -> Windows -> Windows Forms Application) and declares an ITest variable in the main function:

static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());

    ITest xxx = null;
}

The program will run in the development computer (installed visual studio) but crash in two other computers. One of the crashed computers is physical machine and the other one is an virtual machine.

I am using Windows XP Pro SP3, Visual Studio 2010 and .Net Framework 4.0.


Have you checked to make sure the right version of the Microsoft Visual C++ Runtime is installed on the target machines? (Your development environment will already have this installed, but no current version of Windows includes this runtime by default).

If you set up your C++/CLI project to use "SAFE" mode, it will not reference the Microsoft Visual c++ runtime at all (just .NET). See this for reference: Pure and Verifiable Code. If you need to do native stuff, then there's a very high chance that you need to have the latest Visual C++ runtime installed. You can pick up the redistributable here: Microsoft Visual C++ 2010 Redistributable Package.

If you want to verify that this is the problem, you can use the sxstrace tool to diagnose these issues (helpful tutorial).


The null assigment is probably not the error but the trigger for the error. This assigment to null is probably first line of code that accesses that the C++/CLI assembly. So before the that null assigment is even executed, the unmanaged part of C++/CLI assembly is initialized. That is probably where the error is occuring.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜