eglMakeCurrent failed EGL_BAD_ALLOC eglMakeCurrent failed EGL_BAD_ALLOC android android

eglMakeCurrent failed EGL_BAD_ALLOC


If you look at the EGL specification, there are several possible causes for this error. It seems like something in your application is causing it to run out of resources. The spec states the following:

3.7.3 Binding Contexts and Drawables

... eglMakeCurrent binds ctx to the current rendering thread and to the draw and read surfaces...

Errors

... If the ancillary buffers for draw and read cannot be allocated, an EGL_BAD_ALLOC error is generated...

To troubleshoot the issue, you could inspect the memory usage of your application. There are many different techniques to investigate ram usage of your application, some techniques are documented quite well in this guide.

This post also describes that the error is triggered when calling eglMakeCurrent if the EGL_WIDTH and EGL_HEIGHT parameters of the pixel buffer are not set when calling eglCreatePbufferSurface. Here is a minimal java sample to create a pixel buffer (full source located here), ensure that the input width and height are greater than zero:

private void eglSetup(int width, int height) {    mEGL = (EGL10)EGLContext.getEGL();    mEGLDisplay = mEGL.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);    if (!mEGL.eglInitialize(mEGLDisplay, null)) {        throw new RuntimeException("unable to initialize EGL10");    }    // Configure EGL for pbuffer and OpenGL ES 2.0.  We want enough RGB bits    // to be able to tell if the frame is reasonable.    int[] attribList = {            EGL10.EGL_RED_SIZE, 8,            EGL10.EGL_GREEN_SIZE, 8,            EGL10.EGL_BLUE_SIZE, 8,            EGL10.EGL_SURFACE_TYPE, EGL10.EGL_PBUFFER_BIT,            EGL10.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,            EGL10.EGL_NONE    };    EGLConfig[] configs = new EGLConfig[1];    int[] numConfigs = new int[1];    if (!mEGL.eglChooseConfig(mEGLDisplay, attribList, configs, 1, numConfigs)) {        throw new RuntimeException("unable to find RGB888+pbuffer EGL config");    }    // Configure context for OpenGL ES 2.0.    int[] attrib_list = {            EGL14.EGL_CONTEXT_CLIENT_VERSION, 2,            EGL10.EGL_NONE    };    mEGLContext = mEGL.eglCreateContext(mEGLDisplay, configs[0], EGL10.EGL_NO_CONTEXT,        attrib_list);    checkEglError("eglCreateContext");    if (mEGLContext == null) {        throw new RuntimeException("null context");    }    // Create a pbuffer surface.  By using this for output, we can use glReadPixels    // to test values in the output.    int[] surfaceAttribs = {            EGL10.EGL_WIDTH, width,            EGL10.EGL_HEIGHT, height,            EGL10.EGL_NONE    };    mEGLSurface = mEGL.eglCreatePbufferSurface(mEGLDisplay, configs[0], surfaceAttribs);    checkEglError("eglCreatePbufferSurface");    if (mEGLSurface == null) {        throw new RuntimeException("surface was null");    }    mEGL.eglMakeCurrent(mEGLDisplay, mEGLSurface, mEGLSurface, mEGLContext);}

It is difficult to pinpoint the exact cause without knowing more details on the implementation of your application. This should be a good starting point to identify and fix the issue.


This EGL error is typically caused by device-specific hardware acceleration issues. Webviews may be especially susceptible to this error. Try disabling hardware acceleration for problematic views. If the error persists, make sure you’re properly hooking into the Activity lifecycle because that can also cause an EGL allocation crash. Specifically, check to see if onPause & onResume are called on your GLSurfaceView when called on the enclosing Activity.

Resources:

  1. CreateWindowSurface failed EGL_BAD_ALLOC
  2. GLSurfaceView EGL_BAD_ALLOC
  3. GLSurfaceView in Android 2.3.x consistently produces EGL_BAD_ALLOC