Memory AccessViolationException Error Calling DLL From C#
<p>I'm encountering a strange memory read/write error while calling a compiled DLL from C#. I use DllImport to get a handle to the function we need, which writes a return value to a parametric pointer to an int (i.e., int* out). This function is called multiple times within a thread, and runs successfully over the execution life of the first thread. However, if one launches another thread after the first has completed, the call to the external dll throws an AccessViolationException. Since the multiple calls from the first thread execute successfully, I'm thinking this is somehow related to the first thread not releasing the pointers to the relevant integer parameters(?). If so, how can I explicitly release that memory? Or, perhaps someone has a different insight into what might be going on here? Thank you very much.</p>

<p>EDIT: Danny has requested more specifics, and I'm happy to oblige. Here is where the external routine is invoked:</p>

<pre><code>    int success = -1;
    unsafe
    {
        int res = 0;
        int* res_ptr = &amp;res;
        int len = cmd.ToCharArray().Length;
        int* len_ptr = &amp;len;
        CmdInterpreter(ref cmd, len_ptr, res_ptr);
        success = *res_ptr;
    }
</code></pre>

<p>Where CmdInterpreter is defined as:</p>

<pre><code>    [DllImport("brugs.dll", EntryPoint="CmdInterpreter", 
        ExactSpelling=false, CallingConvention = CallingConvention.StdCall)]
    public static unsafe extern void CmdInterpreter(ref string cmd, int *len, int *res);
</code></pre>

<p>Please let me know if there is any additional info that would be helpful. Thank you!</p>
