Skip to content
This repository has been archived by the owner on Feb 22, 2022. It is now read-only.

Commit

Permalink
Fix an occasional crash caused by race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
openaphid committed Dec 7, 2012
1 parent d056dc2 commit 3d24fc8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
24 changes: 13 additions & 11 deletions FlipView/FlipLibrary/src/com/aphidmobile/flip/ViewDualCards.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public View getView() {
return viewRef != null ? viewRef.get() : null;
}

public boolean setView(int index, View view) {
public synchronized boolean setView(int index, View view) {
UI.assertInMainThread();

if (this.index == index
Expand All @@ -71,21 +71,19 @@ && getView() == view
}
if (view != null) {
viewRef = new WeakReference<View>(view);
UI.recycleBitmap(screenshot);
recycleScreenshot();
screenshot = GrabIt.takeScreenshot(view);
} else {
UI.recycleBitmap(screenshot);
screenshot = null;
recycleScreenshot();
}

return true;
}

void markForceReload() {
synchronized void markForceReload() {
UI.assertInMainThread();

UI.recycleBitmap(screenshot);
screenshot = null;
recycleScreenshot();
if (texture != null) {
texture.postDestroy();
texture = null;
Expand All @@ -108,13 +106,12 @@ public Card getBottomCard() {
return bottomCard;
}

public void buildTexture(FlipRenderer renderer, GL10 gl) {
public synchronized void buildTexture(FlipRenderer renderer, GL10 gl) {
if (screenshot != null) {
if (texture != null)
texture.destroy(gl);
texture = Texture.createTexture(screenshot, renderer, gl);
UI.recycleBitmap(screenshot);
screenshot = null;
recycleScreenshot();

topCard.setTexture(texture);
bottomCard.setTexture(texture);
Expand Down Expand Up @@ -181,12 +178,17 @@ public void buildTexture(FlipRenderer renderer, GL10 gl) {
}
}

public void abandonTexture() {
public synchronized void abandonTexture() {
texture = null;
}

@Override
public String toString() {
return "ViewDualCards: (" + index + ", view: " + getView() + ")";
}

private void recycleScreenshot() {
UI.recycleBitmap(screenshot);
screenshot = null;
}
}
8 changes: 6 additions & 2 deletions FlipView/FlipLibrary/src/com/aphidmobile/utils/UI.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ public static void assertInMainThread() {
}

public static void recycleBitmap(Bitmap bm) {
if (bm != null)
bm.recycle();
if (bm != null) {
if (bm.isRecycled())
AphidLog.w("Bitmap is recycled already?");
else
bm.recycle();
}
}

public static <T> T callInMainThread(Callable<T> call) throws Exception {
Expand Down

0 comments on commit 3d24fc8

Please sign in to comment.