Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SurfaceHolder.lockCanvas() Returns null Even Though it Was Available

I have an android game on the market and I've been getting crash reports of NullPointers when trying to use the canvas. I can assume it's because SurfaceHolder.lockCanvas() is returning null, however it does this mid gameplay because based on where it crashes, SurfaceHolder.lockCanvas() returned a valid canvas at least once.

This is difficult to debug because I can't re-create it on my own device, which makes me wonder if it has to do with specific devices. The only hint I have is that one of the devices it occurred on was a Nexus 7.

NOTE: This is not the same problem as the similar-named question by me. The other question was due to trying to use the canvas before it was available, whereas here it was available.

Below is a sample of my code:

public class GameView extends SurfaceView implements SurfaceHolder.Callback
{
    class GameThread extends Thread
    {
        @Override
        public void run()
        {
            while (running)
            {
                Canvas c = null;
                try
                {
                    c = mSurfaceHolder.lockCanvas();

                    synchronized (mSurfaceHolder)
                    {
                        long start = System.currentTimeMillis();
                        doDraw(c);
                        long diff = System.currentTimeMillis() - start;

                        if (diff < frameRate)
                            Thread.sleep(frameRate - diff);
                    }
                } catch (InterruptedException e)
                {
                }
                finally
                {
                    if (c != null)
                    {
                        mSurfaceHolder.unlockCanvasAndPost(c);
                    }
                }
            }
        }
    }

    public void surfaceCreated(SurfaceHolder holder)
    {
        if (gThread.getState() == Thread.State.TERMINATED)
        {
            gThread = new GameThread(getHolder(), getContext(), getHandler());
            gThread.start();
        }
        else
        {
            gThread.start();
        }
    }
}
like image 465
Jesse Jashinsky Avatar asked Nov 14 '22 00:11

Jesse Jashinsky


1 Answers

I also faced that problem. It happens sporadically when the surface is already destroyed (and surfaceDestroyed was already called), but the looper thread is already inside the while loop (and after checking the running variable). Then lockCanvas returns null (as the surface was destroyed). This may happen for example on screen orientation changes or activity changes. It is a "simple" race condition threading problem.

A simple fix is to add if (canvas == null) continue; after lockCanvas (or just break the loop). Then running will be checked again and the loop ends.

like image 73
Zardoz Avatar answered Jan 09 '23 03:01

Zardoz