title: Access violation exception
body: <p>I have a c# program that calls a C dll which in turn calls java dll( built using jet excelsior a 3rd party program). I am passing an xml string from the c# into the java and the java then returns the string to the the c# to process. </p>
  
  <p>This works on the first iteration, but then on the second iteration I get the following exception...</p>
  
  <blockquote>
    <p>Attempted to read or write protected memory. This is often an
    indication that other memory is corrupt.</p>
  </blockquote>
  
  <p>Here is what I would class as the relevant code but if you require anything else please let me know. </p>
  
  <p>C# Calls to C dll</p>
  
  <pre><code>public static class DllCall
  {
      [DllImport("Stubs", CallingConvention = CallingConvention.Cdecl)]
      public static extern int initDll([MarshalAs(UnmanagedType.LPStr)] string userDllName);
  
      [DllImport("Stubs", CallingConvention = CallingConvention.Cdecl)]
      public static extern void finalizeDll();
  
      [DllImport("Stubs", CallingConvention = CallingConvention.Cdecl)]
      public static extern UInt32 newClassInstance(String rootPath, String cfgPath, String logPath );
  
      [DllImport("Stubs", CallingConvention = CallingConvention.Cdecl)]
      public static extern String request(UInt32 hClassInst, [MarshalAs(UnmanagedType.LPStr)] String input);
  
      [DllImport("Stubs", CallingConvention = CallingConvention.Cdecl)]
      public static extern void close();
  
  
  }
  </code></pre>
  
  <p>Method in C dll that is throwing the error</p>
  
  <pre><code>const char* request(jobject obj, char* input )
  {
      jstring inputString;
      jstring outputString;
      const char *nativeString;
  
  
      jmethodID mID = (*env)-&gt;GetMethodID (env, jClass, "request", "(Ljava/lang/String;)Ljava/lang/String;");
      if (!mID){
          printf("\nError: dllClass.request() not found\n");
          return 0;
      }
  
      inputString = (*env)-&gt;NewStringUTF(env, input);
  
  
      outputString = (*env)-&gt;CallObjectMethod(env, obj, mID, inputString);    
  
      nativeString = (*env)-&gt;GetStringUTFChars(env, outputString, 0); 
  
  
      return nativeString;
  }
  </code></pre>
  
  <p>As requested here is the C# code that actually causes the exception.</p>
  
  <pre><code>        public string request(string xmlInput)
  {
      LogManager.logMessage("Sending request to Java. Request is - " + xmlInput);
      string rs ="";
      Console.Write("Making request");
      //this works fine
      rs = DllCall.request(hClass, xmlInput); 
      Console.Write("---&gt; request() rs = {0}\n", rs);
      // this throws the error
      rs = DllCall.request(hClass, "&lt;?xml version='1.0' encoding='utf-8'?&gt;&lt;moo&gt;&lt;request name=\"Panel.Open.GetSelectionTemplate\"/&gt;&lt;/moo&gt;"); 
      return rs;
  }
  </code></pre>
  
  <p>In Response to Daniel here is where env is declared</p>
  
  <pre><code>#include &lt;jni.h&gt;
  #include &lt;windows.h&gt;
  
  JNIEnv  *env;
  JavaVM  *jvm;
  HANDLE  hUserDll;
  jclass  jClass;
  char*  dllname;
  </code></pre>
  
  <p>And here is how it is initialised.</p>
  
  <pre><code>int initDll(char* userDllName) 
  {
    jClass = NULL;
    hUserDll = loadDll(userDllName); 
    dllname = userDllName;
    initJavaRT(hUserDll, &amp;jvm, &amp;env); 
    jClass = lookForClass(env, "XActMain/XActGeminiX3/XActGeminiX3IFX");
    return jClass ? 1 : 0;
  }
  
  /*
   * Initialize JET run-time.
   */
  void initJavaRT(HANDLE myDllHandle, JavaVM** pjvm, JNIEnv** penv)
  {
      int            result;
      JavaVMInitArgs args;
  
      JNI_GetDefaultJavaVMInitArgs_func = 
               (jint (JNICALL *) (void *args))
               GetProcAddress (myDllHandle, "JNI_GetDefaultJavaVMInitArgs");
  
      JNI_CreateJavaVM_func =
               (jint (JNICALL *) (JavaVM **pvm, void **penv, void *args))
               GetProcAddress (myDllHandle, "JNI_CreateJavaVM");
  
      if(!JNI_GetDefaultJavaVMInitArgs_func) {
          printf ("%s doesn't contain public JNI_GetDefaultJavaVMInitArgs\n", dllname);
          exit (1);
      }
  
      if(!JNI_CreateJavaVM_func) {
          printf ("%s doesn't contain public JNI_CreateJavaVM\n", dllname);
          exit (1);
      }
  
      memset (&amp;args, 0, sizeof(args));
      args.version = JNI_VERSION_1_2;
  
      result = JNI_GetDefaultJavaVMInitArgs_func(&amp;args);
      if (result != JNI_OK) {
          printf ("JNI_GetDefaultJavaVMInitArgs() failed with result %d\n", result);
          exit(1);
      }
  
      /*
       * NOTE: no JVM is actually created
       * this call to JNI_CreateJavaVM is intended for JET RT initialization
       */
      result = JNI_CreateJavaVM_func (pjvm, (void **)penv, &amp;args);
      if (result != JNI_OK) {
          printf ("JNI_CreateJavaVM() failed with result %d\n", result);
          exit(1);
      }
  
      printf ("JET RT initialized\n");
      fflush (stdout);
  }
  </code></pre>
  
  <p>This is in response to Joes comment about initialisation...</p>
  
  <pre><code>public class Test
   {
       public UInt32 hClass;
  
       public Test()
       {
          initDll();
          newClassInstance(rootConfig, config, logFile);
       }
  
       ........
  
       public void newClassInstance(string rootPath, string cfgPath, string logPath)
       {
           hClass = DllCall.newClassInstance(rootPath, cfgPath, logPath);
           Console.Write("---&gt; hClass = {0}\n", hClass);
       }
       public void initDll()
       {
               int rc = DllCall.initDll("dllClass.dll");
               Console.Write("---&gt; initDll() rc = {0}\n", rc);
       }
  </code></pre>
  
  <p>Hans has pointed out the following link <a href="https://stackoverflow.com/questions/7294089/allocation-and-deallocation-of-memory-in-unmanaged-code-using-platform-invoke-c">potential answer</a></p>
  
  <p>I am however not sure how to modify my current code to accomodate this solution. </p>
  
  <p>As I say it works once then crashes on the second iteration.</p>