title: Camera.startPreview crashes and reboots phone after multiple runs of Activity
body: <p>I have an <code>Activity</code> that opens the <code>Camera</code> and starts a preview on a <code>SurfaceTexture</code>.  Everything works fine, but I've noticed that if I repeatedly leave the activity and return to it, after a handful of times, the phone freezes and then reboots.  </p>
  
  <p>I've narrowed the problem down to the call to startPreview.  And I get two ominous log messages right before the issue happens:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>Has anyone seen this before?  Is this an issue with the Galaxy Nexus hardware or Android 4.0?  If so, are there any work arounds?</p>
  
  <p>Note: My testing is on Android 4.0, with a Galaxy Nexus.</p>
  
  <p><strong>Edit - Solved:</strong></p>
  
  <p>Turns out it was a memory leak due to OpenGL.  All the examples on the web I could find use the following code to clean up after OpenGL.</p>
  
  <pre><code>try { mEgl.eglDestroyContext(mEglDisplay, mEglContext); } catch (Throwable t) {}
  try { mEgl.eglDestroySurface(mEglDisplay, mEglSurface); } catch (Throwable t) {}
  </code></pre>
  
  <p>This leaks the surface, and as a result after some number of tries, which varies per phone, will cause OpenGL to fail to initialize.  For instance it would fail after 32 tries on the Nexus S, but only 8 tries on the LG Optimus.</p>
  
  <p>After some trial and error I found that the following code fixed the issue:</p>
  
  <pre><code>mEgl.eglMakeCurrent(mEglDisplay, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT);
  </code></pre>
  
  <p>Note: On the Galaxy Nexus instead of getting a nice OpenGL error which I could display to the user it just seemed to crash on startPreview.  I assume this was memory related, but the above fixed cleared it up as well.</p>