Android IllegalArgumentException and Fatal signal 6 (SIGABRT) after call to unlockCanvasAndPost in SurfaceView
<p>I cannot figure out this problem for the life of me. Any help would be greatly appreciated.</p>

<p>I have a class called <code>AndroidFastRenderView</code> that extends <code>SurfaceView</code> and implements <code>Runnable</code>. The thread is created once, and it's supposed to draw onto the SurfaceView until it's destroyed. As you can see from the code below, I am checking to make sure the SurfaceView's holder has a valid Surface and that the Canvas can be locked before attempting to draw on it. However, I still get an <code>IllegalArgumentException</code> when attempting to call <code>holder.unlockCanvasAndPost()</code> (which should never be called if the canvas wasn't already locked).</p>

<pre><code>07-19 17:38:32.350 E/Surface: queueBuffer (handle=0x7ffeec230f50) failed (Operation not permitted)
07-19 17:38:32.350 E/AndroidRuntime: FATAL EXCEPTION: Thread-4
    Process: com.mycompany.myapp, PID: 10103
    java.lang.IllegalArgumentException
        at android.view.Surface.nativeUnlockCanvasAndPost(Native Method)
        at android.view.Surface.unlockSwCanvasAndPost(Surface.java:331)
        at android.view.Surface.unlockCanvasAndPost(Surface.java:312)
        at android.view.SurfaceView$4.unlockCanvasAndPost(SurfaceView.java:954)
        at com.mycompany.framework.implementation.AndroidFastRenderView.run(AndroidFastRenderView.java:109)
        at java.lang.Thread.run(Thread.java:761)
</code></pre>

<p>This is causing some very strange behavior. First of all, it results in a <code>Fatal error 6 (SIGABRT)</code>, which is just altogether unhelpful. Here's what it looks like:</p>

<pre><code>07-19 17:38:32.350 A/libc: Invalid address 0x7ffed9d58400 passed to free: value not allocated
07-19 17:38:32.350 A/libc: Fatal signal 6 (SIGABRT), code -6 in tid 10136 (Thread-4)
</code></pre>

<p>However, I have a sneaking suspicion that this error is unrelated, because I also get this <code>SIGABRT</code> message when I attempt to use a <code>MediaPlayer</code> to play music. Those error messages look very similar:</p>

<pre><code>07-19 17:27:43.736 A/libc: Invalid address 0x7ffed8395000 passed to free: value not allocated
07-19 17:27:43.736 A/libc: Fatal signal 6 (SIGABRT), code -6 in tid 9906 (NDK MediaCodec_)
07-19 17:27:43.737 A/libc: Invalid address 0x7ffed83a9400 passed to free: value not allocated
07-19 17:27:43.737 A/libc: Fatal signal 6 (SIGABRT), code -6 in tid 9882 (SoundPoolThread)
</code></pre>

<p>In both of these cases, I also get some strange error messages about a buffer that make no sense.</p>

<pre><code>07-19 17:38:33.693 1246-1246/? E/Layer: rejecting buffer: bufWidth=1777, bufHeight=1077, front.active.{w=225, h=225}
07-19 17:38:33.791 1246-1246/? E/Layer: rejecting buffer: bufWidth=1664, bufHeight=1077, front.active.{w=225, h=225}
</code></pre>

<p>And for some reason that I cannot understand, when I comment out the single line that calls <code>holder.lockCanvas()</code>, these errors completely go away (but then of course the screen is blank!). Suffice it to say I am completely dumbfounded. I have been working on this for days and I've made zero headway.</p>

<p>Here is the code for the <code>run</code> method in my <code>AndroidFastRenderView</code> class.</p>

<pre><code>@Override
public void run() {
    while(running &amp;&amp; !Thread.currentThread().isInterrupted()) {
        if (!holder.getSurface().isValid())
            continue;

        float deltaTime = (System.nanoTime() - startTime) / 10000000.000f;
        startTime = System.nanoTime();

        if (deltaTime &gt; 5) {
            deltaTime = (float) 5;
        }

        game.getCurrentScreen().update(deltaTime);
        game.getCurrentScreen().paint(deltaTime);

        Canvas canvas = null;
        try {
            canvas = holder.lockCanvas();
        } catch (Exception e) {
            Log.d(LOG_TAG, "Failed to lock canvas.");
        }
        if (canvas != null) {
            synchronized (holder) {
                if (dstRect.right == 1 &amp;&amp; dstRect.bottom == 1) {
                    dstRect.right = canvas.getWidth();
                    dstRect.bottom = canvas.getHeight();
                }

                // canvas.getClipBounds(dstRect);
                // canvas.drawARGB(255, 0, 0, 0);
                canvas.drawBitmap(bgFrameBuffer.getBitmap(), null, dstRect, paint);
                canvas.drawBitmap(mainFrameBuffer.getBitmap(), null, dstRect, paint);
                canvas.drawBitmap(fgFrameBuffer.getBitmap(), null, dstRect, paint);

                holder.unlockCanvasAndPost(canvas); /* &lt;-- the line that throws the IllegalArgumentException */
            }
        }
    }
}
</code></pre>

<p>And here is the full logcat for any souls brave enough to sift through the junk.</p>

<pre><code>07-19 17:38:28.529 10103-10136/com.mycompany.myapp D/MyAppDebug: Assets loaded!
07-19 17:38:28.529 10103-10136/com.mycompany.myapp D/MyAppDebug: Counter1
07-19 17:38:28.550 10103-10138/com.mycompany.myapp I/OMXClient: MuxOMX ctor
07-19 17:38:28.833 10103-10141/com.mycompany.myapp I/OMXClient: MuxOMX ctor
07-19 17:38:29.090 1246-1246/? E/Layer: [Starting com.mycompany.myapp] rejecting buffer: bufWidth=1440, bufHeight=2392, front.active.{w=1, h=1}
07-19 17:38:29.106 10103-10144/com.mycompany.myapp I/OMXClient: MuxOMX ctor
07-19 17:38:29.251 3951-3986/system_process I/WindowManager: Screen frozen for +3s679ms due to Window{c73d173 u0 Starting com.mycompany.myapp}
07-19 17:38:29.432 10103-10147/com.mycompany.myapp I/OMXClient: MuxOMX ctor
07-19 17:38:29.491 10103-10151/com.mycompany.myapp I/OMXClient: MuxOMX ctor
07-19 17:38:30.048 10103-10154/com.mycompany.myapp I/OMXClient: MuxOMX ctor
07-19 17:38:30.536 10103-10157/com.mycompany.myapp I/OMXClient: MuxOMX ctor
07-19 17:38:32.350 10103-10136/com.mycompany.myapp E/Surface: queueBuffer: error queuing buffer to SurfaceTexture, -1
07-19 17:38:32.350 10103-10136/com.mycompany.myapp E/Surface: queueBuffer (handle=0x7ffeec230f50) failed (Operation not permitted)
07-19 17:38:32.350 10103-10136/com.mycompany.myapp E/AndroidRuntime: FATAL EXCEPTION: Thread-4
                                                                           Process: com.mycompany.myapp, PID: 10103
                                                                           java.lang.IllegalArgumentException
                                                                               at android.view.Surface.nativeUnlockCanvasAndPost(Native Method)
                                                                               at android.view.Surface.unlockSwCanvasAndPost(Surface.java:331)
                                                                               at android.view.Surface.unlockCanvasAndPost(Surface.java:312)
                                                                               at android.view.SurfaceView$4.unlockCanvasAndPost(SurfaceView.java:954)
                                                                               at com.mycompany.framework.implementation.AndroidFastRenderView.run(AndroidFastRenderView.java:109)
                                                                               at java.lang.Thread.run(Thread.java:761)
07-19 17:38:32.350 10103-10136/com.mycompany.myapp A/libc: Invalid address 0x7ffed9d58400 passed to free: value not allocated
07-19 17:38:32.350 10103-10136/com.mycompany.myapp A/libc: Fatal signal 6 (SIGABRT), code -6 in tid 10136 (Thread-4)

                                                                 [ 07-19 17:38:32.350  1181: 1181 W/         ]
                                                                 debuggerd: handling request: pid=10103 uid=10081 gid=10081 tid=10136
07-19 17:38:32.376 10160-10160/? A/DEBUG: pid: 10103, tid: 10136, name: Thread-4  &gt;&gt;&gt; com.mycompany.myapp &lt;&lt;&lt;
07-19 17:38:32.878 3951-3986/system_process I/ActivityManager: Displayed com.mycompany.myapp/.MyAppPaid: +7s536ms
07-19 17:38:33.073 3951-3986/system_process I/WindowManager: Destroying surface Surface(name=Starting com.mycompany.myapp) called by com.android.server.wm.WindowStateAnimator.destroySurface:1992 com.android.server.wm.WindowStateAnimator.destroySurfaceLocked:885 com.android.server.wm.WindowState.destroyOrSaveSurface:2065 com.android.server.wm.AppWindowToken.destroySurfaces:359 com.android.server.wm.WindowStateAnimator.finishExit:558 com.android.server.wm.WindowStateAnimator.stepAnimationLocked:484 com.android.server.wm.WindowAnimator.updateWindowsLocked:303 com.android.server.wm.WindowAnimator.animateLocked:704 
07-19 17:38:33.116 3951-10165/system_process W/ActivityManager:   Force finishing activity com.mycompany.myapp/.MyAppPaid
07-19 17:38:33.120 3951-4076/system_process W/InputDispatcher: channel '4176eea com.mycompany.myapp/com.mycompany.myapp.MyAppPaid (server)' ~ Consumer closed input channel or an error occurred.  events=0x9
07-19 17:38:33.121 3951-4076/system_process E/InputDispatcher: channel '4176eea com.mycompany.myapp/com.mycompany.myapp.MyAppPaid (server)' ~ Channel is unrecoverably broken and will be disposed!
07-19 17:38:33.137 3951-4245/system_process I/WindowManager: WIN DEATH: Window{4176eea u0 com.mycompany.myapp/com.mycompany.myapp.MyAppPaid}
07-19 17:38:33.137 3951-4245/system_process W/InputDispatcher: Attempted to unregister already unregistered input channel '4176eea com.mycompany.myapp/com.mycompany.myapp.MyAppPaid (server)'
07-19 17:38:33.137 3951-4245/system_process W/WindowManager: Force-removing child win Window{57133bc u0 SurfaceView - com.mycompany.myapp/com.mycompany.myapp.MyAppPaid} from container Window{4176eea u0 com.mycompany.myapp/com.mycompany.myapp.MyAppPaid}
07-19 17:38:33.137 3951-4245/system_process I/WindowManager: Destroying surface Surface(name=SurfaceView - com.mycompany.myapp/com.mycompany.myapp.MyAppPaid) called by com.android.server.wm.WindowStateAnimator.destroySurface:1992 com.android.server.wm.WindowStateAnimator.destroySurfaceLocked:885 com.android.server.wm.WindowState.removeLocked:1447 com.android.server.wm.WindowManagerService.removeWindowInnerLocked:2477 com.android.server.wm.WindowManagerService.removeWindowInnerLocked:2456 com.android.server.wm.WindowManagerService.removeWindowLocked:2435 com.android.server.wm.WindowState$DeathRecipient.binderDied:1772 android.os.BinderProxy.sendDeathNotice:688 
07-19 17:38:33.146 3951-4245/system_process I/WindowManager: Destroying surface Surface(name=com.mycompany.myapp/com.mycompany.myapp.MyAppPaid) called by com.android.server.wm.WindowStateAnimator.destroySurface:1992 com.android.server.wm.WindowStateAnimator.destroySurfaceLocked:885 com.android.server.wm.WindowState.removeLocked:1447 com.android.server.wm.WindowManagerService.removeWindowInnerLocked:2477 com.android.server.wm.WindowManagerService.removeWindowLocked:2435 com.android.server.wm.WindowState$DeathRecipient.binderDied:1772 android.os.BinderProxy.sendDeathNotice:688 &lt;bottom of call stack&gt; 
07-19 17:38:33.161 3951-4203/system_process I/ActivityManager: Process com.mycompany.myapp (pid 10103) has died
07-19 17:38:33.693 1246-1246/? E/Layer: [Application Error: com.mycompany.myapp] rejecting buffer: bufWidth=1777, bufHeight=1077, front.active.{w=225, h=225}
07-19 17:38:33.791 1246-1246/? E/Layer: [Application Error: com.mycompany.myapp] rejecting buffer: bufWidth=1664, bufHeight=1077, front.active.{w=225, h=225}
</code></pre>