title: Proper binding of a native library in Xamarin
body: <p>Given this method in a <code>c++</code> library (<a href="https://github.com/wasabeef/gifflen-sample" rel="noreferrer">Gifflen, animated gif maker</a>):</p>
  
  <pre><code>JNIEXPORT jint JNICALL Java_org_jiggawatt_giffle_Giffle_Init(JNIEnv *ioEnv, jobject ioThis, 
  jstring gifName, jint w, jint h, jint numColors, jint quality, jint frameDelay) { ... }
  </code></pre>
  
  <p>And this signature to access it:</p>
  
  <pre><code>extern "C"
  {
      JNIEXPORT jint JNICALL Java_org_jiggawatt_giffle_Giffle_Init(JNIEnv *ioEnv,
      jobject ioThis, jstring gifName, jint w, jint h, jint numColors, jint quality, 
      jint frameDelay);
  }
  </code></pre>
  
  <p>Shouldn't the p/invoke be similar to that?</p>
  
  <pre><code>[DllImport("libgifflen.so", CharSet = CharSet.Ansi)]
  public static extern int Java_org_jiggawatt_giffle_Giffle_Init(string gifName, int w, 
  int h, int numColors, int quality, int frameDelay);
  </code></pre>
  
  <p>I have a program that basically just tries to access that Init methods.</p>
  
  <p>I'm loading the library successfully: <code>Java.Lang.JavaSystem.LoadLibrary("gifflen");</code></p>
  
  <p>Then it seems I'm pointing to the correct method, but it crashes as soon as it enters in the method.</p>
  
  <p>I'm calling Init with same values as in the java demo application (and with basically the same values as in all my other implementations of this gif encoder since this is the same code-base for all libraries):</p>
  
  <pre><code>var pathForResult = "/storage/sdcard0/giftest/giffleResult.gif";
  int optCol = 256, optQuality = 100, optDelay = 4;
  Giffle.Init (pathForResult , 256, 256, optCol, optQuality, optDelay);
  </code></pre>
  
  <p>Then I'm getting this crash:</p>
  
  <pre><code>[mono-rt]   at &lt;unknown&gt; &lt;0xffffffff&gt;
  [mono-rt]   at (wrapper managed-to-native) org.jiggawatt.giffle.Giffle.Java_org_jiggawatt_giffle_Giffle_Init (string,int,int,int,int,int) &lt;IL 0x00046, 0xffffffff&gt;
  [mono-rt]   at org.jiggawatt.giffle.Giffle.Init (string,int,int,int,int,int) [0x00001] in c:\Users\user.name\Projects\Giffle\Giffle\Giffle.cs:20
  [mono-rt] 
  [mono-rt]   Attempting native Android stacktrace:
  [mono-rt] 
  [mono-rt]   at Java_org_jiggawatt_giffle_Giffle_Init+19 [0x6433c83c]
  [mono-rt]   at ???+12272 [0x6467fff0]
  [libc] Fatal signal 11 (SIGSEGV) at 0x6f7475d3 (code=1), thread 29670 (myapp.someapp)
  </code></pre>
  
  <p>I'm I using native libraries incorrectly in my Xamarin application?</p>
  
  <blockquote>
    <p>Code for bindings</p>
  </blockquote>
  
  <pre><code>using System;
  using System.Runtime.InteropServices;
  
  namespace org.jiggawatt.giffle
  {
      public class Giffle {
  
          public const string GIFFLE_DLL_NAME = "libgifflen.so";
  
          [DllImport(GIFFLE_DLL_NAME, CharSet = CharSet.Ansi)]
          public static extern int Java_org_jiggawatt_giffle_Giffle_Init(string gifName, int w, int h, int numColors, int quality, int frameDelay);
  
          [DllImport(GIFFLE_DLL_NAME, CharSet = CharSet.Ansi)]
          public static extern void Java_org_jiggawatt_giffle_Giffle_Close();
  
          [DllImport(GIFFLE_DLL_NAME, CharSet = CharSet.Ansi)]
          public static extern int Java_org_jiggawatt_giffle_Giffle_AddFrame(int[] inArray);
      }
  }
  </code></pre>
  
  <blockquote>
    <p>Code for test</p>
  </blockquote>
  
  <pre><code>var external = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
  var appPath = System.IO.Path.Combine (external, "giftest");
  if (!Directory.Exists (appPath)) {
      Directory.CreateDirectory (appPath);
  }
  
  var t = "/storage/sdcard0/giftest/result3.gif";
  
  File.Delete (t);
  int optCol = 256, optQuality = 100, optDelay = 4;
  
  Giffle.Init (t, 256, 256, optCol, optQuality, optDelay);
  var image = Bitmap.CreateBitmap (256, 256, Bitmap.Config.Argb8888);
  for (int i = 0; i &lt; 50; i++) {
      image = Bitmap.CreateBitmap (256, 256, Bitmap.Config.Argb8888);
      image.EraseColor (Color.Argb (255, 255 - i * 4, i * 4, i * 4));
  
      int[] pixelsCopy = new int[image.Width * image.Height];
      image.LockPixels ();
      image.GetPixels(pixelsCopy, 0, image.Width, 0, 0, image.Width, image.Height);
      image.UnlockPixels ();
      image.Recycle ();
      Giffle.Java_org_jiggawatt_giffle_Giffle_AddFrame (pixelsCopy);
  }
  
  Giffle.Java_org_jiggawatt_giffle_Giffle_Close ();
  </code></pre>