Grant Bartel
Grant Bartel

Reputation: 363

Attempted to read or write protected memory exception occurs w/o debugging but not w/debugging

Derivatives of this question have surely been asked, however I have yet to find one that matches my case.

I created a library that is wrapping up a third party C++ library and supplying it to some in-house systems. The created library uses C++/CLI to allows users to access it via C#. The library builds and can be launched using our internal continuous integration NuGet server, therefore it's available for use.

There is no problem using this library within some other application/system when running a program with "Start Debugging" (F5), but whenever it's run with "Start Without Debugging" (Ctrl+F5) or via an executable the following exception is raised:

Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

at TheirLib.func(Int32 , Int32 , Double* , Int32 , Int32* , Int32* , Double* , Int32 , TheirLibError* )
at MyLib.Func(Int32 n, Int32 m, Double[] x, Int32 tdx, Int32[] svar, Int32[] sobs, Double[] corr, Int32 tdc)
at SomeApplication.Program.Main(String[] args) in d:\SomeApplication\SomeApplication\Program.cs:line 67

I read here (https://connect.microsoft.com/VisualStudio/feedback/details/819552/visual-studio-debugger-throws-accessviolationexception) that it could be that one needs .NET 4.5.2, but this didn't change anything. I also thought that there might be some setting in my C++/CLI library that would allow me to circumvent this issue, but I haven't been able to find anything and I feel like I've looked everywhere. I also looked at the C++/CLI code preparing the pin_ptr objects that are used when calling the third party library, but everything looks fine (see below).

void MyLib::Func(int n, int m, array<double>^ x, int tdx, array<int>^ svar, array<int>^ sobs,
    array<double>^ corr, int tdc)
{
    // Setup input parameters
    pin_ptr<double> xPtr = &(x[0]);
    pin_ptr<int> svarPtr = &(svar[0]);
    pin_ptr<int> sobsPtr = &(sobs[0]);
    pin_ptr<double> corrPtr = &(corr[0]);
    TheirLibError fail;

    // Call their library's function
    func(n, m, xPtr, tdx, svarPtr, sobsPtr, corrPtr, tdc, &fail);
}

Again, the library works exactly as expected with "Start Debugging" and passes an exhaustive set of tests, but I just cannot understand why this wouldn't work standalone... I will literally try anything at this point to get it working. Help me SO, you're my only hope...

Upvotes: 2

Views: 116

Answers (1)

Grant Bartel
Grant Bartel

Reputation: 363

After contacting the library owner, the issue was that TheirLibError was not being initialized properly, and the pointers contained within this structure were not being handled properly within the library. After properly initializing it, everything worked as expected.

Moral of the story, inquire with the library owner regarding whether or not their library holds onto pointers or any other pointer behavior when approached with an AccessViolationException.

Upvotes: 1

Related Questions