title: STATUS_HEAP_CORRUPTION error occurred when send data from Rust to C# DLL
body: <p>I have been facing an issue when I send the string data from Rust to C# DLL. Rust exe will show the following error.</p>
  <p><code>error: process didn't exit successfully: target\release\test.exe (exit code: 0xc0000374, STATUS_HEAP_CORRUPTION)</code></p>
  <p>The Rust exe will crash after I sent the data for a few times (No exact pattern).</p>
  <p>Below is the code on how the Rust communicate with C# DLL.</p>
  <pre><code>extern crate libloading;
  
  fn call_dynamic(arg_1_str: &amp;str, arg_2_int: i32) -&gt; Result&lt;i32, Box&lt;dyn std::error::Error&gt;&gt; {
  
      let arg_1_c_str = CString::new(arg_str_1).unwrap();
  
      // This is intended 
      let arg_3_c_str = CString::new(&quot;&quot;).unwrap();
  
      let arg_1_c_char = arg_1_c_str.as_ptr();
      let arg_3_c_char = arg_3_c_str.as_ptr();
  
      let lib = libloading::Library::new(&quot;Test.dll&quot;).unwrap();
      unsafe {
          let func: libloading::Symbol&lt;
              unsafe fn(
                  arg_1: *const c_char,
                  arg_2: i32,
                  arg_3: *const c_char,
              ) -&gt; i32,
          &gt; = lib
              .get(b&quot;GetStatus&quot;)
              .unwrap();
  
          Ok(func(arg_1_c_char, arg_2_int, arg_3_c_char))
      }
  }
  </code></pre>
  <p>The library that used to communicate with external DLL is libloading.</p>
  <p>What could possible cause this error to be happened and is there any way to overcome the issue? Your help is much appreciated.</p>
  <p><strong>Note:</strong> I have no access to the source code of the C# DLL. Hence, I have no idea what is happening inside.</p>
  <p>Here's some Java code that works. I'm trying to do the same thing in Rust:</p>
  <pre class="lang-java prettyprint-override"><code>public interface TestDLL extends Library { 
      int GetStatus (String arg1, int arg2, String arg3);
  }
  
  public int GetStatusFromDLL (String arg1, int arg2) {
      TestDLL test_dll = (TestDLL)Native.loadLibrary (&quot;Test&quot;, TestDLL.class);
      return test_dll.GetStatus (arg1, arg2, null);
  }
  </code></pre>