AccessViolationException while copying data from C++ to C#
Hello!
I'm writing a multithreaded program in C#/WPF which is using program code, written in C++. I'm using MS Visual Studio 2010 and I'm building project for x64 platform, .NET 4.0.
I have 2 projects in my solution:
1. C++ project, which is built into DLL with "/clr", and has native C++ code and Visual C++ to call native C++ from C#.
2. C# project, which is drawing windows and control multithreading.
The idea is simple - native C++ program gets initial data, makes some calculations (about several seconds) and output results array with a lot of data. I need to make a lot of these calculations for various initial data. So I made it like this:
•C# parse file with initial data
•C# launches a parallel.for cycle:
--------------------------------------------------------------------------------
Code:
parallelOptions.MaxDegreeOfParallelism = 8; // for 100% load on 8 cores
Parallel.For(0, data.NUM_TRAJECTORIES, parallelOptions, i => {
// call Visual C++ to call native C++
});
--------------------------------------------------------------------------------
•In unsafe ('cause I need pointers in C# to fill C# array from C++) void function C# creates an array for results:
Code:
data.dataFromCPP[i] = new double[result.GetSize(), 8]; // 8 - is the number of output parameters. result.GetSize() - returns number of rows for current finished calculation
•Using pointer to array, C++ fills C# array with output data:
Code:
fixed (double* p = data.dataFromCPP[i])
{
result.CopyResults(p);
}
--------------------------------------------------------------------------------
And here's the function from native C++:
Code:
void cSolver::copyResults(double *externalArray)
{
for(int i = 0 ; i < resultsVector.size(); i++)
{
externalArray[i*8 + 0] = resultsVector[i].lat;
externalArray[i*8 + 1] = resultsVector[i].lon;
externalArray[i*8 + 2] = resultsVector[i].altitude;
externalArray[i*8 + 3] = resultsVector[i].tendency;
externalArray[i*8 + 4] = resultsVector[i].time;
externalArray[i*8 + 5] = resultsVector[i].Vx;
externalArray[i*8 + 6] = resultsVector[i].Vy;
externalArray[i*8 + 7] = resultsVector[i].Vz;
}
}
The Problem:
The problem is in AccessViolationException while copying data from C++ to C#, last time it occurred on this line
Code:
externalArray[i*8 + 3] = resultsVector[i].tendency;
It appears rarely - I've launched program 4 times, for the first 3 times it done everything excellent, and on the fourth it stopped with this exception. But that's really bad, 'cause for 3200 calculations - it takes about 20 minutes to calculate them all. And when it is stopped on 3197' trajectory - it is reeeealy terrible...
So, can anyone help me with this problem? What am I doing wrong, and what should I do to avoid this exception.
Best regards,
Nikita Petrov.
Re: AccessViolationException while copying data from C++ to C#
From MSDN
Quote:
An access violation occurs in unmanaged or unsafe code when the code attempts to read or write to memory that has not been allocated, or to which it does not have access. This usually occurs because a pointer has a bad value. Not all reads or writes through bad pointers lead to access violations, so an access violation usually indicates that several reads or writes have occurred through bad pointers, and that memory might be corrupted. Thus, access violations almost always indicate serious programming errors. In the .NET Framework version 2.0, an AccessViolationException clearly identifies these serious errors.
In programs consisting entirely of verifiable managed code, all references are either valid or null, and access violations are impossible. An AccessViolationException occurs only when verifiable managed code interacts with unmanaged code or with unsafe managed code.
I would suggest you to make a simple program with your native actual code, which can reproduce the issue (such as simple Win32 console application). Probably, while doing this exercise either you'll figure out, if not, then post the project/code snippet here for others, who might help you out.
Re: AccessViolationException while copying data from C++ to C#
Quote:
Originally Posted by
nikkitta
It appears rarely - I've launched program 4 times, for the first 3 times it done everything excellent, and on the fourth it stopped with this exception.
The apparently indeterministic nature of the bug suggests that it might be a race condition, i.e. some inter-thread synchronization issue. I happened to hit one of those myself just a few days ago, and that simply semells alike...
For reproducing this kind of bug, the few lines of code you posted are by far not enough. So I strongly suggest Ejaz' suggestion to try to set up a small sample that reproduces the problem "in closed circuit", and also agree that chances are you'll find out about the bug yourself while doing so.