bilibili / boxing

Android multi-media selector based on MVP mode.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

BoxingCrop.INSTANCE 造成内存泄露

itsgm opened this issue · comments

commented

图片裁剪leaknary会检测到内存泄露,是不是应该在activity或fragment销毁的时候做处理?

commented

具体堆栈信息是?

commented

com.bumptech.glide.manager.SupportRequestManagerFragment has leaked:

  • static BoxingCrop.!(INSTANCE)!
  • ↳ BoxingCrop.!(mCrop)!
  • ↳ InformationActivity$3.!(this$0)! (anonymous implementation of com.bilibili.boxing.loader.IBoxingCrop)
  • ↳ InformationActivity.mFragments
  • ↳ FragmentController.mHost
  • ↳ FragmentActivity$HostCallbacks.mFragmentManager
  • ↳ FragmentManagerImpl.mAdded
  • ↳ ArrayList.elementData
  • ↳ array Object[].[0]
  • ↳ SupportRequestManagerFragment
commented
  • com.tbruyelle.rxpermissions2.RxPermissionsFragment has leaked:
  • static BoxingCrop.!(INSTANCE)!
  • ↳ BoxingCrop.!(mCrop)!
  • ↳ InformationActivity$3.!(this$0)! (anonymous implementation of com.bilibili.boxing.loader.IBoxingCrop)
  • ↳ InformationActivity.mFragments
  • ↳ FragmentController.mHost
  • ↳ Activity$HostCallbacks.mFragmentManager
  • ↳ FragmentManagerImpl.mAdded
  • ↳ ArrayList.elementData
  • ↳ array Object[].[1]
  • ↳ RxPermissionsFragment
commented

↳ InformationActivity$3.!(this$0)! (anonymous implementation of com.bilibili.boxing.loader.IBoxingCrop)

是否在activity/fragment内用匿名内部类实现了IBoxingCrop,这样会导致泄露

commented

是的,我初始化的时候用的匿名内部类。
BoxingMediaLoader.getInstance().init(new IBoxingMediaLoader() {
@OverRide
public void displayThumbnail(@nonnull ImageView img, @nonnull String absPath, int width, int height) {
String path = "file://" + absPath;
try {
// bumptech/glide#1531
GlideApp.with(img.getContext()).load(path)
.placeholder(R.drawable.default_user)
.centerCrop()
.override(width, height).into(img);
} catch (IllegalArgumentException ignore) {
}
}

        @Override
        public void displayRaw(@NonNull ImageView img, @NonNull String absPath, int width, int height, IBoxingCallback callback) {
            String path = "file://" + absPath;

// BitmapTypeRequest request =
GlideRequest request = GlideApp
.with(img.getContext())
.load(path);
if (width > 0 && height > 0) {
request.override(width, height);
}
request.listener(new RequestListener() {
@OverRide
public boolean onLoadFailed(@nullable GlideException e, Object model, Target target, boolean isFirstResource) {
if (callback != null) {
callback.onFail(e);
return true;
}
return false;
}

                @Override
                public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                    if (resource != null && callback != null) {
                        img.setImageDrawable(resource);
                        callback.onSuccess();
                        return true;
                    }
                    return false;
                }
            }).into(img);
        }
    });

    BoxingCrop.getInstance().init(new IBoxingCrop() {
        @Override
        public void onStartCrop(Context context, Fragment fragment, @NonNull BoxingCropOption cropConfig, @NonNull String path, int requestCode) {
            Uri uri = new Uri.Builder()
                    .scheme("file")
                    .appendPath(path)
                    .build();
            UCrop.Options crop = new UCrop.Options();
            // do not copy exif information to crop pictures
            // because png do not have exif and png is not Distinguishable
            crop.setCompressionFormat(Bitmap.CompressFormat.PNG);
            crop.withMaxResultSize(cropConfig.getMaxWidth(), cropConfig.getMaxHeight());
            crop.withAspectRatio(cropConfig.getAspectRatioX(), cropConfig.getAspectRatioY());

            UCrop.of(uri, cropConfig.getDestination())
                    .withOptions(crop)
                    .start(context, fragment, requestCode);
        }

        @Override
        public Uri onCropFinish(int resultCode, Intent data) {
            if (data == null) {
                return null;
            }
            Throwable throwable = UCrop.getError(data);
            if (throwable != null) {
                return null;
            }
            return UCrop.getOutput(data);
        }
    });
commented

改成静态内部类或者在Application内初始化