|
-
May 16th, 2012, 03:07 AM
#1
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|