title: not valid JNI reference using NewGlobalReference in android
body: <p>Because of limited android application memory heap I am trying to create by cache array of bitmaps in C using malloc and memcpy.</p>
  
  <p>So in java side I have:</p>
  
  <pre><code>private static native Bitmap getJNIBitmap(int id);
  private static native void setJNIBitmap(int id, Bitmap bmp);
  </code></pre>
  
  <p>to send bitmap to C:</p>
  
  <pre><code>Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
  setJNIBitmap(id, bmp);
  </code></pre>
  
  <p>to retrieve it from C:</p>
  
  <pre><code>Bitmap bitmap = getJNIBitmap(id);
  </code></pre>
  
  <p>In C code I have an array which I want to use a cache of bitmaps:</p>
  
  <pre><code>jobject bitmap_array[100];
  </code></pre>
  
  <p>To save bitmap in C I use:</p>
  
  <pre><code>JNIEXPORT void JNICALL Java_droid_demo_ReadingActivity_setJNIBitmap(JNIEnv* env, jobject obj, jint id, jobject jniBmp) {
  
    AndroidBitmapInfo info;
    AndroidBitmap_getInfo(env, jniBmp, &amp;info);
  
    long byteSize = (info.stride * info.height)+16;
  
    bitmap_array[(int)id] = (jobject *)malloc(byteSize);
    memcpy(bitmap_array[(int)id], jniBmp, byteSize);
    (*env)-&gt;NewGlobalRef(env, bitmap_array[(int)id]);
  }
  </code></pre>
  
  <p>To retrieve bitmap from C:</p>
  
  <pre><code>JNIEXPORT jobject JNICALL Java_droid_demo_ReadingActivity_getJNIBitmap(JNIEnv * env, jobject obj, jint id) {
  
    jobject temp = bitmap_array[(int)id];
  
    return temp;
  }
  </code></pre>
  
  <p>I am green at using JNI and C in general, but in my head this should work. Well it doesn't and I receive:</p>
  
  <pre><code>10-05 10:35:54.890: W/dalvikvm(12493): JNI WARNING: 0x5d5e0008 is not a valid JNI reference
  10-05 10:35:54.890: W/dalvikvm(12493):              in Ldroid/demo/ReadingActivity;.setJNIBitmap:(ILandroid/graphics/Bitmap;)V (NewGlobalRef)
  10-05 10:35:54.890: I/dalvikvm(12493): "main" prio=5 tid=1 RUNNABLE
  10-05 10:35:54.890: I/dalvikvm(12493):   | group="main" sCount=0 dsCount=0 obj=0x41611568 self=0x416019f8
  10-05 10:35:54.890: I/dalvikvm(12493):   | sysTid=12493 nice=0 sched=0/0 cgrp=apps handle=1074710320
  10-05 10:35:54.890: I/dalvikvm(12493):   | schedstat=( 7665497000 93442000 221 ) utm=418 stm=348 core=1
  </code></pre>
  
  <p>Any ideas on why I cannot make bitmap_array[(int)id] a global reference? And maybe even how to solve this problem?</p>