Search code examples
javaandroidmultithreadingopengl-esglsurfaceview

How to do clean up in native heap when opengl context lost in GLSurfaceview in Android?


I'm making a wrapper for a native 3d engine. and I used GLSurfaceView to establish egl context for my engine.

The problem is that, when GLSurfaceView lose its egl context (onPause, etc), I really wanna free the memory the engine used. However, there seems no way to do it safely.

I've try to override function onPause in GLSurfaceView like below:

@Override
public void onPause(){
Engine.release();
super.onPause();
}

However, it will cause memory problem.

04-06 21:33:40.415: E/libEGL(8249): call to OpenGL ES API with no current context (logged once per thread)
04-06 21:33:40.425: A/libc(8249): Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1)

It might due to the memory is freed but the renderer thread is still running.

I think the best way to do clean up is to free memory in renderer thread just before it ends. But I don't know how to implements it.

Could someone help me?


Solution

  • GLSurfaceView does its own EGL context management. It takes all responsibility for creating it, destroying it, and ensuring that it's current on the renderer thread when onDrawFrame() is called.

    If this is not what you want, you should use a plain SurfaceView instead, and issue the various EGL calls yourself.