LunarLander: Fix race condition causing crash on exit

When exiting the LunarLander sample, it was possible for the canvas
to get destroyed before execution was halted.

Added additional synchronization to make sure the current run-state
is accounted for before performing a canvas update.

Also updated deprecated constants in lunar_layout.xml.

Bug: 6164549
Change-Id: I9a710f6b128491f1beece84dd39ab00f4937a4c6
This commit is contained in:
Trevor Johns
2012-10-31 16:58:00 -07:00
parent 42d7eefb86
commit b69d09ecd9
3 changed files with 23 additions and 11 deletions

View File

@@ -149,8 +149,8 @@ public class LunarLander extends Activity {
*/
@Override
protected void onPause() {
super.onPause();
mLunarView.getThread().pause(); // pause game when Activity pauses
super.onPause();
}
/**

View File

@@ -196,6 +196,8 @@ class LunarView extends SurfaceView implements SurfaceHolder.Callback {
/** Indicate whether the surface has been created & is ready to draw */
private boolean mRun = false;
private final Object mRunLock = new Object();
/** Scratch rect object. */
private RectF mScratchRect;
@@ -356,7 +358,13 @@ class LunarView extends SurfaceView implements SurfaceHolder.Callback {
c = mSurfaceHolder.lockCanvas(null);
synchronized (mSurfaceHolder) {
if (mMode == STATE_RUNNING) updatePhysics();
doDraw(c);
// Critical section. Do not allow mRun to be set false until
// we are sure all canvas draw operations are complete.
//
// If mRun has been toggled false, inhibit canvas operations.
synchronized (mRunLock) {
if (mRun) doDraw(c);
}
}
} finally {
// do this in a finally so that if an exception is thrown
@@ -427,7 +435,11 @@ class LunarView extends SurfaceView implements SurfaceHolder.Callback {
* @param b true to run, false to shut down
*/
public void setRunning(boolean b) {
mRun = b;
// Do not allow mRun to be modified while any canvas operations
// are potentially in-flight. See doDraw().
synchronized (mRunLock) {
mRun = b;
}
}
/**