Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: move bitmap recycling to onDetachedFromWindow #1542

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1284,6 +1284,8 @@ public void onChildViewAdded(View view, View view1) {
@Override
public void onChildViewRemoved(View view, View view1) {
if (view instanceof VirtualView) {
SvgView svgView = ((VirtualView) view).getSvgView();
svgView.setRemovedFromReactViewHierarchy();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested your code and there is a chance that svgView is null. I would suggest doing a null check here - just to avoid potential crashes.

invalidateSvgView((VirtualView) view);
}
}
Expand Down
19 changes: 19 additions & 0 deletions android/src/main/java/com/horcrux/svg/SvgView.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public String toString() {
}

private @Nullable Bitmap mBitmap;
private boolean mRemovedFromReactViewHierarchy;

public SvgView(ReactContext reactContext) {
super(reactContext);
Expand All @@ -78,6 +79,10 @@ public void setId(int id) {
SvgViewManager.setSvgView(id, this);
}

public void setRemovedFromReactViewHierarchy() {
mRemovedFromReactViewHierarchy = true;
}

@Override
public void invalidate() {
super.invalidate();
Expand All @@ -90,6 +95,20 @@ public void invalidate() {
((VirtualView) parent).getSvgView().invalidate();
return;
}
if (!mRemovedFromReactViewHierarchy) {
// when view is removed from the view hierarchy, we want to recycle the mBitmap when
// the view is detached from window, in order to preserve it for during animation, see
// https://github.com/react-native-svg/react-native-svg/pull/1542
if (mBitmap != null) {
mBitmap.recycle();
}
mBitmap = null;
}
}

@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
if (mBitmap != null) {
mBitmap.recycle();
}
Expand Down