Camera.startPreview crashes and restarts the phone after several starts of Activity

I have an Activity that opens Camera and starts a preview on SurfaceTexture . Everything works fine, but I noticed that if I leave the work many times and return to it, after a few minutes the phone freezes and then reboots.

I narrowed the problem down to calling startPreview. And I get two sinister log messages right before the problem occurs:

 01-19 10:20:52.038: E/IMGSRV(22777): :0: __map: Map device memory failed 01-19 10:20:52.038: W/GraphicBufferMapper(22777): registerBuffer(0x70b750) failed -14 (Bad address) 

Has anyone seen this before? Is this a hardware problem with the Galaxy Nexus or Android 4.0? If so, is there any work around?

Note. My testing on Android 4.0 with Galaxy Nexus.

Edit - Solved:

Turns out it was a memory leak due to OpenGL. All the examples on the Internet that I could find use the following code to clean up after OpenGL.

 try { mEgl.eglDestroyContext(mEglDisplay, mEglContext); } catch (Throwable t) {} try { mEgl.eglDestroySurface(mEglDisplay, mEglSurface); } catch (Throwable t) {} 

This is a surface leak, and as a result, after a number of attempts that change for each phone, this will cause OpenGL to fail to initialize. For example, it will fail after 32 attempts on the Nexus S, but only 8 attempts on the LG Optimus.

After some trial and error, I found that the following code fixed the problem:

 mEgl.eglMakeCurrent(mEglDisplay, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT); 

Note. In Galaxy Nexus, instead of getting a good OpenGL error that I could display to the user, it just seemed to crash in startPreview. I assume this is memory related, but the above fix has also been fixed.

+4
source share
1 answer

The docs SDKs for the android.hardware.Camera class say the following:

Important. Call release() to free the camera for use by other Applications. Applications should immediately release the onPause() camera (and ref open() it in onResume() ).

Are you sure your activity releases the camera in onPause() and reopens it in onResume() ?

If you post a sample code, we can better fix your problem.

+2
source

Source: https://habr.com/ru/post/1391888/


All Articles