JNI- unpinned primitve array error
<p>I want to implement invert effect in image processing. I decode color channels in java side the I pass a 2D array to C side, I invert (255-value) then I return a processed 2D array.</p>

<p>Here is my C code:</p>

<pre><code>   #include &lt;jni.h&gt;
#include&lt;stddef.h&gt;
#include &lt;stdio.h&gt;
#include&lt;com_example_invert_MainActivity.h&gt;
JNIEXPORT jobjectArray JNICALL Java_com_example_invert_MainActivity_inv
  (JNIEnv *env, jobject obj, jobjectArray arr, jint w, jint h)
{
    double a[w][h][3];
        int i,j,k;
        double x = 0;
        ///////////////////READING THE INPUT ARRAY////////////////////////


         jsize dim1 = (*env)-&gt;GetArrayLength(env, arr);

           for (i=0; i&lt;dim1; i++){

                jdoubleArray *line1 =   (*env)-&gt;GetObjectArrayElement(env, arr, i);
                int dim2 =       (*env)-&gt;GetArrayLength(env, line1);
                jdouble *pos1 = (*env)-&gt;GetDoubleArrayElements(env, line1, 0);

                for (j=0; j&lt;dim2; j++){
                    jdoubleArray *line2 =   (*env)-&gt;GetObjectArrayElement(env, line1, j);
                    int dim3 =       (*env)-&gt;GetArrayLength(env, line2);
                    jdouble *pos2 = (*env)-&gt;GetDoubleArrayElements(env, line2, 0);

                    for (k=0; k&lt;dim3; k++){
                             a[i][j][k]= pos2[k];
                        }
                    (*env)-&gt;ReleaseDoubleArrayElements(env, arr, pos2, 0);
                    (*env)-&gt;ReleaseDoubleArrayElements(env, arr, line2, 0);
                  }

                (*env)-&gt;ReleaseDoubleArrayElements(env, arr, pos1, 0);

                (*env)-&gt;ReleaseDoubleArrayElements(env, arr, line1, 0);
           }



        /////////////////PROCESSING...///////////////////


        for( i = 0; i&lt;w; i++){
            for( j = 0; j&lt;h; j++){
                for( k = 0; k&lt;3; k++){

                    a[i][j][k] = 255-a[i][j][k];
                }
            }
        }
        //////////////RETURNING THE ARRAY////////////////////////////
        jclass doubleArrayArrayClass = (*env)-&gt;FindClass(env,"[[D");
                jclass doubleArrayClass = (*env)-&gt;FindClass(env,"[D");

                jobjectArray ret  = (*env)-&gt;NewObjectArray(env,w, doubleArrayArrayClass, NULL);
        for( i = 0; i&lt;w; i++){
            jobjectArray dim2 = (*env)-&gt;NewObjectArray(env, w, doubleArrayClass, NULL);
            for( j = 0; j&lt;h; j++) {
                jdoubleArray dim1 = (*env)-&gt;NewDoubleArray(env,h);
                jdouble tmp[256];
                for( k = 0; k&lt;3; k++){
                    tmp[k] = a[i][j][k];
                }
                (*env)-&gt;SetDoubleArrayRegion(env,dim1 , 0, 3, tmp);
                (*env)-&gt;SetObjectArrayElement(env, dim2, j, dim1);
                (*env)-&gt;DeleteLocalRef(env, dim1);
            }
            (*env)-&gt;SetObjectArrayElement(env,ret, i, dim2);
            (*env)-&gt;DeleteLocalRef(env,dim2);
        }
        return ret;
}
</code></pre>

<p>And here is the java code:</p>

<pre><code>public class MainActivity extends ActionBarActivity {
ImageView imageView2;
double[][][] imgArray;
int w,h;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    imageView2 = (ImageView) findViewById(R.id.imageView1);
    imageView2.setDrawingCacheEnabled(true);
    BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView2.getDrawable();
    final Bitmap bitmap = bitmapDrawable.getBitmap();
    Button button = (Button) findViewById(R.id.button1);


    w = bitmap.getWidth();
    h = bitmap.getHeight();
    imgArray = new double[w][h][3];

   for(int i = 0 ; i&lt;w; i++){
        for(int j =0; j&lt;h; j++){
            imgArray[i][j][0] = Color.red(bitmap.getPixel(i, j));
            imgArray[i][j][1] = Color.green(bitmap.getPixel(i, j));
            imgArray[i][j][2] = Color.blue(bitmap.getPixel(i, j));
        }
    }

    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            imgArray = inv(imgArray, w, h);
            Bitmap newBitmap = Bitmap.createBitmap(w,h,bitmap.getConfig());
            for(int i = 0 ; i&lt;w; i++){
                for(int j =0; j&lt;h; j++){
                    newBitmap.setPixel(i, j, Color.rgb((int)(imgArray[i][j][0]), (int)(imgArray[i][j][1]), (int)(imgArray[i][j][2])));
                }
           }
            imageView2.setImageBitmap(newBitmap);
        }
    });

}
static{
    System.loadLibrary("inv");
}

// internal, private
public native double[][][] inv(double[][][] inputArr, int w, int h);


    ...
}
</code></pre>

<p>The application crashes with logcat error: <code>W/dalvikvm(5009): JNI: unpinPrimitiveArray(0x424eaea0) failed to find entry (valid=1)</code></p>