title: Application crashing due to use of ANativeWindow API
body: <p>I'm calling my native function that uses <code>ANativeWindow API</code> from the java code from an <code>UI</code> thread as shown below:</p>
  
  <pre><code>MainActivity.this.runOnUiThread(new Runnable() {
  
                      @Override
                      public void run() {
                          // your stuff to update the UI
                          mainFunction(3,inputFile,outputFile,surface);
                          Log.d(TAG, "Thread executed successfully");
                      }
                  });
  </code></pre>
  
  <p>Then in the native code while copying the data from the rgba buffer to the bits, the application is carshing. Native code snippet is shown below:</p>
  
  <pre><code>void yuv2rgba(UINT8* y,UINT8* u,UINT8* v,int w, int h,FILE *fp_Out_File,jobject surface)
  {
       int mem1=w*h,i=0,j=0,k=0;
  __android_log_print(ANDROID_LOG_DEBUG,"MYDECODER","yuv2rgba is entered.....",NULL);
  
      JNIEnv *env;
      ANativeWindow* window;
      ANativeWindow_Buffer buffer;
  //buffer1: holds the rgba data
      UINT8 *buffer1=(UINT8*)malloc(sizeof(UINT8)*(4*mem1));
      UINT8 *r=(UINT8*)malloc(sizeof(UINT8)*mem1);
      UINT8 *g=(UINT8*)malloc(sizeof(UINT8)*mem1);
      UINT8 *b=(UINT8*)malloc(sizeof(UINT8)*mem1);
      int y1,u1,v1,rcomp,gcomp,bcomp;
  
      int getEnvStat = (*g_vm)-&gt;GetEnv(g_vm,(void **)&amp;env, JNI_VERSION_1_6);
      if (getEnvStat == JNI_EDETACHED) {
      __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","GetEnv: not attached.....",NULL);
      }
      else if (getEnvStat == JNI_OK) {
  
          __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","getEnvStat == JNI_OK.....",NULL);
      }
      else if (getEnvStat == JNI_EVERSION) {
  
          __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","GetEnv: version not supported.....",NULL);
      }
  
      __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","computing rgb.....",NULL);
  
      for(i=0;i&lt;mem1;++i)
       {
          y1=y[i]-16;
          u1=u[i]-128;
          v1=v[i]-128;
  
          rcomp=((298 * y1) +( 409 * v1))&gt;&gt;8;
          r[i]=(UINT8)((rcomp&lt;0)?0:((rcomp &gt; 255)?255:rcomp));
  
          gcomp=((298 * y1) - (100 *u1) - (208 * v1))&gt;&gt;8;
          g[i]=(UINT8)((gcomp&lt;0)?0:((gcomp &gt; 255)?255:gcomp));
          bcomp=((298 * y1) + (516 * u1))&gt;&gt;8;
          b[i]=(UINT8)((bcomp&lt;0)?0:((bcomp &gt; 255)?255:bcomp));
      }
      __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","assigning rgba to buffer....",NULL);
      // Storing the RGB value into a buffer
      for(i=0,j=0;(i&lt;(mem1*4))&amp;&amp;(j&lt;mem1);i=i+4,++j)
         {
          buffer1[i]=r[j];
          buffer1[i+1]=g[j];
          buffer1[i+2]=b[j];
          buffer1[i+3]=0xff;
         }
      free(r);
      free(g);
      free(b);
      //fwrite to an output file
      //fwrite(buffer1,sizeof(UINT8),w*h*3,fp_Out_File);
  
  
      __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","before creating a window.....",NULL);
  
       window = ANativeWindow_fromSurface(env, surface);
  
       __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","window created.....",NULL);
  
       if (ANativeWindow_lock(window, &amp;buffer, NULL) == 0) {
  
         memcpy(buffer.bits, buffer1,  sizeof(UINT8)*(4*mem1));
  
         __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","memcpy successfull.....",NULL);
  
         ANativeWindow_unlockAndPost(window);
  
         __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","unlock and post  successful..." ,NULL);
       }
       ANativeWindow_release(window);
       __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","release successful.....",NULL);
  
      free(buffer1);
  }
  </code></pre>
  
  <p>When the application is run, the application crashes after the comment: <strong>"window created...."</strong>. along with the below message in the logcat:</p>
  
  <pre><code>02-18 14:46:55.344: I/dalvikvm(2080): threadid=4: reacting to signal 3
  02-18 14:46:55.348: I/dalvikvm(2080): Wrote stack traces to '/data/anr/traces.txt'
  </code></pre>
  
  <p>In the above code my aim is to display the rgba data present in <code>buffer1</code> on to the device screen using the <code>ANativeWindow</code> API</p>