简体   繁体   中英

Unhandled Exception: System.AccessViolationException : attempted to read or write protected

Problem: Getting an error when running my .exe

An unhandled exception of type 'System.AccessViolationException' occurred in AddingWrapper.dll

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

In the console it writes this:

Unhandled Exception: System.AccessViolationException : attempted to read or write protected memory. This is often an indication that other memory is corrupt. at gcroot (Add ^).. P$AAVAdd@@(gcroot(Add^) ) at AddingWrapper.Adding(AddingWrapper , Int32* x, Int32* y)

Code snippet:

VB code:

Public Class Add
  Public Function Adding(ByVal x As Double, ByVal y As Double) As Integer
      Return x + y
   End Function

End Class

AddingWrapper.h:

#pragma once
#include "stdafx.h"

class AddingWrapperPrivate;


class __declspec(dllexport) AddingWrapper {

private: AddingWrapperPrivate* _private;
public: AddingWrapper();
        int Adding(int* x, int* y);
       ~AddingWrapper();

};

AddingWrapper.cpp

#include "stdafx.h"

#include "AddingWrapper.h"

#using "Class1.dll"
#include <msclr\auto_gcroot.h>
using namespace System::Runtime::InteropServices;

class AddingWrapperPrivate {
  public: msclr::auto_gcroot<Add^> add;
};

AddingWrapper::AddingWrapper()
{
    _private = new AddingWrapperPrivate();
    _private->add = gcnew Add();
};
int AddingWrapper::  Adding(int* x, int* y) {
    return _private->add->Adding(*x, *y);
};
AddingWrapper::~AddingWrapper()
{
    delete _private;
};

calling code:

 #include "stdafx.h"
 #include "AddingWrapper.h"
 #include <iostream>
    int main()
{
  int *a = 0;
   int *b = 0;
   AddingWrapper *add;
   int results =  add->Adding(a,b);
   std::cout << "here is the result";
   std::cout << results;
   return 0;
 }

Could it be due to my Class1.dll in AddingWrapper.cpp is using VB.net? Or it's a question of other issues? All the other threads seem to all differ in answer (ie one is suggesting the user account doesn't have all the rights to the computer). If ever I missed on of those thread, please link it to me, this error is killing me

I should also add this error is at run time not compile time.

Several things:

  • You haven't shown your VB code. Since you've written an unmanaged class, not a managed one, it seems likely that either the import is not correct, or that you're passing a bad pointer.
  • Why are you passing an int* to the wrapper, only to dereference it right there? Why not pass an int ?
  • You're in C++/CLI, why are you not writing a managed class? You wouldn't need auto_gcroot , and you don't need to deal with DLL imports/exports: VB.Net would be able to see your class the same as it can see any .Net class, and reference it just as easily as you can reference any .Net library.

Edit

OK, it wasn't obvious that you were trying to call some VB.Net code from C++. I thought you were trying to go the other direction.

The problem is almost certainly that you're passing a bad pointer to AddingWrapper::Adding .

You don't need to pass a pointer for basic data types, so you can get rid of that entire thing if you want. The fact that it's a double in VB but an int in C++ is fine, C++/CLI knows that the VB code takes a double and will convert appropriately.

Also, note that you're not passing a pointer between managed and unmanaged code. You're passing a pointer from one unmanaged class to another unmanaged class (whatever calls AddWrapper, to AddWrapper), but across the managed/unmanaged border, you're passing a plain old int .

In the main function, you are using a "null" object pointer and passing in NULL pointers - that will cause the error you are seeing.

int main()
{
   int a = 1;
   // ^^^ remove the pointer (and give it a "interesting" value)
   int b = 2;
   // ^^^ remove the pointer
   AddingWrapper add; // remove the pointer (or allocate with new)
   //          ^^^ remove the pointer
   int results = add.Adding(&a, &b); // pass in the address of the integers
   //              ^^^ syntax change

   std::cout << "here is the result";
   std::cout << results;
   return 0;
 }

The variable a , b and add where only pointers, pointing to nothing; this causes access violations. Changing them to be automatic objects ("on the stack") will fix this. If dynamic objects are needed, you can new them (and delete them afterwards); but favour library utilities such as std::shared_ptr and std::unique_ptr etc. to help manage the lifetime of the object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM