AccessViolationException while copying data from C++ to C#
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 3 of 3

Thread: AccessViolationException while copying data from C++ to C#

  1. #1
    Join Date
    May 2012

    AccessViolationException while copying data from C++ to C#


    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:
    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:
    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:
    fixed (double* p = data.dataFromCPP[i])
    And here's the function from native C++:
    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

    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.

  2. #2
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Lahore, Pakistan

    Re: AccessViolationException while copying data from C++ to C#

    From MSDN

    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.
    Last edited by Ejaz; May 16th, 2012 at 07:33 AM.

  3. #3
    Join Date
    Jun 2010

    Re: AccessViolationException while copying data from C++ to C#

    Quote Originally Posted by nikkitta View Post
    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.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Azure Activities Information Page

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


HTML5 Development Center