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

Commit 3d24fc8

Browse files
committed
Fix an occasional crash caused by race condition
#15
1 parent d056dc2 commit 3d24fc8

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

FlipView/FlipLibrary/src/com/aphidmobile/flip/ViewDualCards.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public View getView() {
5454
return viewRef != null ? viewRef.get() : null;
5555
}
5656

57-
public boolean setView(int index, View view) {
57+
public synchronized boolean setView(int index, View view) {
5858
UI.assertInMainThread();
5959

6060
if (this.index == index
@@ -71,21 +71,19 @@ && getView() == view
7171
}
7272
if (view != null) {
7373
viewRef = new WeakReference<View>(view);
74-
UI.recycleBitmap(screenshot);
74+
recycleScreenshot();
7575
screenshot = GrabIt.takeScreenshot(view);
7676
} else {
77-
UI.recycleBitmap(screenshot);
78-
screenshot = null;
77+
recycleScreenshot();
7978
}
8079

8180
return true;
8281
}
8382

84-
void markForceReload() {
83+
synchronized void markForceReload() {
8584
UI.assertInMainThread();
8685

87-
UI.recycleBitmap(screenshot);
88-
screenshot = null;
86+
recycleScreenshot();
8987
if (texture != null) {
9088
texture.postDestroy();
9189
texture = null;
@@ -108,13 +106,12 @@ public Card getBottomCard() {
108106
return bottomCard;
109107
}
110108

111-
public void buildTexture(FlipRenderer renderer, GL10 gl) {
109+
public synchronized void buildTexture(FlipRenderer renderer, GL10 gl) {
112110
if (screenshot != null) {
113111
if (texture != null)
114112
texture.destroy(gl);
115113
texture = Texture.createTexture(screenshot, renderer, gl);
116-
UI.recycleBitmap(screenshot);
117-
screenshot = null;
114+
recycleScreenshot();
118115

119116
topCard.setTexture(texture);
120117
bottomCard.setTexture(texture);
@@ -181,12 +178,17 @@ public void buildTexture(FlipRenderer renderer, GL10 gl) {
181178
}
182179
}
183180

184-
public void abandonTexture() {
181+
public synchronized void abandonTexture() {
185182
texture = null;
186183
}
187184

188185
@Override
189186
public String toString() {
190187
return "ViewDualCards: (" + index + ", view: " + getView() + ")";
191188
}
189+
190+
private void recycleScreenshot() {
191+
UI.recycleBitmap(screenshot);
192+
screenshot = null;
193+
}
192194
}

FlipView/FlipLibrary/src/com/aphidmobile/utils/UI.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,12 @@ public static void assertInMainThread() {
4040
}
4141

4242
public static void recycleBitmap(Bitmap bm) {
43-
if (bm != null)
44-
bm.recycle();
43+
if (bm != null) {
44+
if (bm.isRecycled())
45+
AphidLog.w("Bitmap is recycled already?");
46+
else
47+
bm.recycle();
48+
}
4549
}
4650

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

0 commit comments

Comments
 (0)