title: Unhandled Exception: System.AccessViolationException : attempted to read or write protected
body: <p><strong>Problem:</strong> Getting an error when running my .exe </p>
  
  <blockquote>
    <p>An unhandled exception of type 'System.AccessViolationException'
    occurred in AddingWrapper.dll</p>
    
    <p>Additional information: Attempted to read or write protected memory.
    This is often an indication that other memory is corrupt.</p>
  </blockquote>
  
  <p>In the console it writes this: </p>
  
  <blockquote>
    <p>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^)<em>)  at
    AddingWrapper.Adding(AddingWrapper</em>, Int32* x, Int32* y)</p>
  </blockquote>
  
  <p>Code snippet:</p>
  
  <p>VB code:</p>
  
  <pre><code>Public Class Add
    Public Function Adding(ByVal x As Double, ByVal y As Double) As Integer
        Return x + y
     End Function
  </code></pre>
  
  <p>End Class</p>
  
  <p>AddingWrapper.h:</p>
  
  <pre><code>#pragma once
  #include "stdafx.h"
  
  class AddingWrapperPrivate;
  
  
  class __declspec(dllexport) AddingWrapper {
  
  private: AddingWrapperPrivate* _private;
  public: AddingWrapper();
          int Adding(int* x, int* y);
         ~AddingWrapper();
  
  };
  </code></pre>
  
  <p>AddingWrapper.cpp</p>
  
  <pre><code>#include "stdafx.h"
  
  #include "AddingWrapper.h"
  
  #using "Class1.dll"
  #include &lt;msclr\auto_gcroot.h&gt;
  using namespace System::Runtime::InteropServices;
  
  class AddingWrapperPrivate {
    public: msclr::auto_gcroot&lt;Add^&gt; add;
  };
  
  AddingWrapper::AddingWrapper()
  {
      _private = new AddingWrapperPrivate();
      _private-&gt;add = gcnew Add();
  };
  int AddingWrapper::  Adding(int* x, int* y) {
      return _private-&gt;add-&gt;Adding(*x, *y);
  };
  AddingWrapper::~AddingWrapper()
  {
      delete _private;
  };
  </code></pre>
  
  <p>calling code:</p>
  
  <pre><code> #include "stdafx.h"
   #include "AddingWrapper.h"
   #include &lt;iostream&gt;
      int main()
  {
    int *a = 0;
     int *b = 0;
     AddingWrapper *add;
     int results =  add-&gt;Adding(a,b);
     std::cout &lt;&lt; "here is the result";
     std::cout &lt;&lt; results;
     return 0;
   }
  </code></pre>
  
  <p>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 (i.e 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</p>
  
  <p>I should also add this error is at run time not compile time.</p>