title: GC collecting 'pinned' arrays on call to bindings in Xamarin.iOS
body: <p>Afternoon,</p>
  
  <p>I've had the same Xamarin.iOS bindings to native code for roughly 3 years working flawlessly but lately i've noticed a crash behaviour that might have been there since the beginning. I deal with bindings of large (and also small) arrays like images and so on.</p>
  
  <p><strong>The issue</strong>: It seems like the GC is collecting some of the structures while the unmanaged calls to native are operating. I am 'pinning' the data involved and method binding calls with unsafe fixed statements.</p>
  
  <pre><code>fixed (double* px = _px)
  {
      fixed (double* py = _py)
      {
          fixed (double* param = parameters)
          {
              fixed (int* distMap = _distMap)
              {
                  IntPtr dataPxPtr = new IntPtr((void*)px);
                  IntPtr dataPyPtr = new IntPtr((void*)py);
                  IntPtr parametersPtr = new IntPtr((void*)param);
                  IntPtr distMapPtr = new IntPtr((void*)distMap);
                  _bindings.Initiate(dataPxPtr, dataPyPtr, parametersPtr, distMapPtr, 1E-4, Size.Width);
                  _bindings.Iterate();
              }
          }
      }
  }
  </code></pre>
  
  <p>Theoretically the nested fixed statement should pin the memory addresses whilst the binding methods run. But that doesn't seem to be the case...</p>
  
  <p><strong>Xamarin Side Stacktrace</strong></p>
  
  <pre><code>Warning (239) / iOSApp: critical:   at &lt;unknown&gt; &lt;0xffffffff&gt;
  Warning (239) / iOSApp: critical:   at (wrapper managed-to-native) ...Messaging.void_objc_msgSend (intptr,intptr) &lt;0x00007&gt;
  Warning (239) / iOSApp: critical:   at ...Bindings.Iterate () &lt;0x00047&gt;
  </code></pre>
  
  <p><strong>XCode Stacktrace</strong></p>
  
  <pre><code>Exception Type:  EXC_BAD_ACCESS (SIGABRT)
  Exception Subtype: KERN_INVALID_ADDRESS at 0x00000001088fc018
  Triggered by Thread:  4
  
  Thread 4 name:  tid_681f
  Thread 4 Crashed:
  0   libsystem_kernel.dylib          0x0000000182cac11c __pthread_kill + 8
  1   libsystem_pthread.dylib         0x0000000182d78ef8 pthread_kill + 112
  2   libsystem_c.dylib               0x0000000182c1ddc8 abort + 140
  3   iOSApp                          0x0000000100ed26cc mono_handle_native_sigsegv (mini-exceptions.c:2462)
  4   iOSApp                          0x0000000100eddccc mono_sigsegv_signal_handler (mini-runtime.c:2906)
  5   libsystem_platform.dylib        0x0000000182d7194c _sigtramp + 68
  6   iOSApp                          0x00000001000ed77c lmdif_ (lmdif_.c:468)
  7   iOSApp                          0x00000001000f1e44 lmdif1_ (lmdif1_.c:153)
  8   iOSApp                          0x00000001000ec3c8 -[Bindings iterate] (Bindings.m:403)
  9   iOSApp                          0x0000000101bf1698 wrapper_managed_to_native_Bindings_Messaging_void_objc_msgSend_intptr_intptr (/&lt;unknown&gt;:1)
  10  iOSApp                          0x0000000101bf0428 Bindings_Iterate (/&lt;unknown&gt;:1)
  </code></pre>
  
  <p>I've tried, with no success:</p>
  
  <ul>
  <li>Attempting to make deep copies in C# to IntPtr and pass them to bindings</li>
  <li>Attempting to make deep copies in obj-c as soon as they are passed to the bindings</li>
  <li>Attempting pinning of arrays in C# managed side (GCHandle) and use them to pass to the bings (gchandle.free() after obv)</li>
  <li>others i cant remember...</li>
  </ul>
  
  <p>Any help would be appreciated as i'm going nuts.</p>
  
  <p>Thanks,</p>
  
  <p>Antonio</p>