title: Fatal signal 11 and INVALID HEAP ADDRESS IN dlfree error when using glShaderBinary
body: <p>Device : Samsung Galaxy Note
  Android version: 4.0.4</p>
  
  <p>My application apply some visual effects to a video stream using OpenGL Shaders. I need to use binary shaders for security reasons.  I am using <code>glSurfaceView</code> and <code>SurfaceTexture</code> to access the frame data in OpenGL shaders and using the extension <code>GL_OES_EGL_image_external</code>. This is working fine as far as I am using shader source. When I try to use compiled shader binaries I often get an error <code>A/libc(4242): Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1)</code> and the program crashes. When It is working when I try to close the app with back key I usually get the error 
  <code>A/libc(4907): @@@ ABORTING: INVALID HEAP ADDRESS IN dlfree</code></p>
  
  <p>I tried to use binary shaders in the native-media sample application in android xref : 
  <a href="http://androidxref.com/source/xref/development/ndk/platforms/android-14/samples/native-media/" rel="nofollow">http://androidxref.com/source/xref/development/ndk/platforms/android-14/samples/native-media/</a></p>
  
  <p>and I get the same  errors.</p>
  
  <p>I am creating binary shaders using the compiler provided by ARM malisc
  and the code I use for loading the binaries are </p>
  
  <pre><code>private int ShaderFromBIN(Resources resource) {
  
  IntBuffer vert2 = IntBuffer.allocate(1);
          IntBuffer frag2 = IntBuffer.allocate(1);
  
          vert2.put(GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER));
          frag2.put(GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER));
          try {
              InputStream in_sv = resource.openRawResource(R.raw.v2);
              InputStream in_sf = resource.openRawResource(R.raw.f2);
  
              byte[] bv = new byte [in_sv.available()];
              in_sv.read(bv);
              //= IOUtils.toByteArray(in_sv);
              ByteBuffer bbv = ByteBuffer.wrap(bv);
  
              byte[] bf = new byte [in_sf.available()];
              //      IOUtils.toByteArray(in_sf);
              in_sf.read(bf);
              ByteBuffer bbf = ByteBuffer.wrap(bf);
  
              IntBuffer numFormats = IntBuffer.allocate(1);
              GLES20.glGetIntegerv(GLES20.GL_NUM_SHADER_BINARY_FORMATS,
                      numFormats);
              IntBuffer formats;
              if (numFormats.get(0) &gt; 0) {
                  formats = IntBuffer.allocate(numFormats.get(0));
                  GLES20.glGetIntegerv(GLES20.GL_SHADER_BINARY_FORMATS, formats);
  
                  for (int i = 0; i &lt; numFormats.get(0); ++i) {
                      Log.d("format " + i + " ", formats.get(i) + "");
  
                      GLES20.glShaderBinary(1, vert2, formats.get(i), bbv, bv.length);
                      GLES20.glShaderBinary(1, frag2, formats.get(i), bbf, bf.length);
                      checkGlError("compile vertex shader");
                  }
  
              }
              in_sv.close();
              in_sf.close();
              bf = null;
              bv = null;
  
          } catch (Exception e) {
              // e.printStackTrace();
              Log.e("Error: can't read resource.", "");
          }
  
          sampler_program = GLES20.glCreateProgram();
          if (sampler_program != 0) {
              GLES20.glAttachShader(sampler_program, vert2.get(0));
              checkGlError("glAttachShader");
              GLES20.glAttachShader(sampler_program, frag2.get(0));
              checkGlError("glAttachShader");
              GLES20.glLinkProgram(sampler_program);
              checkGlError("glLinkProgram");
              int[] linkStatus = new int[1];
              GLES20.glGetProgramiv(sampler_program, GLES20.GL_LINK_STATUS,
                      linkStatus, 0);
              if (linkStatus[0] != GLES20.GL_TRUE) {
                  Log.e(TAG, "Could not link program: ");
                  Log.e(TAG, GLES20.glGetProgramInfoLog(sampler_program));
                  GLES20.glDeleteProgram(sampler_program);
                  sampler_program = 0;
              }
          }
          checkGlError("after linking");
          if (sampler_program == 0) {
              Log.e(TAG, "Error in creating and compiling the shader programs");
              return -1;
          }
  
          return sampler_program;
      }
  </code></pre>
  
  <p>I am struggling with this problem for 4 days. Am I doing anything wrong? Please help me to sort this issue.</p>
  
  <p>Thank you,</p>
  
  <p>Dhammike</p>
  
  <p>Edit:</p>
  
  <p>When I get the crash log with ndk-stack this is what I get</p>
  
  <p><em><strong></em>***<em></strong> Crash dump: <strong></em>**<em>*</em></strong>  </p>
  
  <pre><code>Build fingerprint: 'samsung/GT-N7000/GT-N7000:4.0.4/IMM76D/XXLRK:user/release-keys'  
  pid: 11581, tid: 11594  &gt;&gt;&gt; com.example.nativemedia &lt;&lt;&lt;  
  signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr deadbaad  
  Stack frame #00  pc 00017938  /system/lib/libc.so  
  Stack frame #01  pc 000137c2  /system/lib/libc.so  
  Stack frame #02  pc 00015b00  /system/lib/libc.so (dlfree)  
  Stack frame #03  pc 00016178  /system/lib/libc.so (free)  
  Stack frame #04  pc 00025e04  /system/lib/libMali.so (__mali_program_binary_state_reset)  
  Stack frame #05  pc 00016068  /system/lib/egl/libGLESv2_mali.so(_gles_program_rendering_state_deref)  
  Stack frame #06  pc 000281b4  /system/lib/egl/libGLESv2_mali.so_gles2_program_internal_free)  
  Stack frame #07  pc 00028228  /system/lib/egl/libGLESv2_mali.so(_gles2_program_object_list_entry_delete)  
  Stack frame #08  pc 0002da9c  /system/lib/egl/libGLESv2_mali.so(_gles_share_lists_clear_v2_content)  
  Stack frame #09  pc 000110d4  /system/lib/egl/libGLESv2_mali.so (_gles_share_lists_deref)  
  Stack frame #10  pc 00026b88  /system/lib/egl/libGLESv2_mali.so (_gles2_delete_context)  
  Stack frame #11  pc 00013efc  /system/lib/egl/libEGL_mali.so (__egl_gles_release_context)  
  Stack frame #12  pc 0000b07c  /system/lib/egl/libEGL_mali.so (__egl_release_context)  
  Stack frame #13  pc 0000b0d4  /system/lib/egl/libEGL_mali.so (_egl_destroy_context_internal)  
  Stack frame #14  pc 0000b6e8  /system/lib/egl/libEGL_mali.so (_egl_destroy_context)  
  Stack frame #15  pc 0000c720  /system/lib/egl/libEGL_mali.so (eglDestroyContext)  
  Stack frame #16  pc 0000c346  /system/lib/libEGL.so (eglDestroyContext)  
  Stack frame #17  pc 0004b67e  /system/lib/libandroid_runtime.so  
  Stack frame #18  pc 0001edb0  /system/lib/libdvm.so (dvmPlatformInvoke) 
  Stack frame #19  pc 00059168  /system/lib/libdvm.so (_Z16dvmCallJNIMethodPKjP6JValuePK6MethodP6Thread)
  </code></pre>