From df87bef96d69397c6fcaeae2e2e43e4761457479 Mon Sep 17 00:00:00 2001 From: toneyzeng Date: Sat, 18 Jun 2022 03:22:05 +0800 Subject: [PATCH] [android] Fix leak of bitmap object (android.graphics.Bitmap of Java) while using platform image decoder. (flutter/engine#33953) --- .../platform/android/android_image_generator.cc | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/engine/src/flutter/shell/platform/android/android_image_generator.cc b/engine/src/flutter/shell/platform/android/android_image_generator.cc index b614f8ebad5..c53e36e3cb4 100644 --- a/engine/src/flutter/shell/platform/android/android_image_generator.cc +++ b/engine/src/flutter/shell/platform/android/android_image_generator.cc @@ -4,6 +4,8 @@ #include "flutter/shell/platform/android/android_image_generator.h" +#include + #include #include @@ -94,11 +96,10 @@ void AndroidImageGenerator::DoDecodeImage() { env, env->NewDirectByteBuffer(const_cast(data_->data()), data_->size())); - fml::jni::ScopedJavaGlobalRef* bitmap = - new fml::jni::ScopedJavaGlobalRef( - env, env->CallStaticObjectMethod( - g_flutter_jni_class->obj(), g_decode_image_method, - direct_buffer.obj(), reinterpret_cast(this))); + auto bitmap = std::make_unique>( + env, env->CallStaticObjectMethod( + g_flutter_jni_class->obj(), g_decode_image_method, + direct_buffer.obj(), reinterpret_cast(this))); FML_CHECK(fml::jni::CheckException(env)); if (bitmap->is_null()) { @@ -127,11 +128,12 @@ void AndroidImageGenerator::DoDecodeImage() { reinterpret_cast*>(context); auto env = fml::jni::AttachCurrentThread(); AndroidBitmap_unlockPixels(env, bitmap->obj()); + delete bitmap; }; software_decoded_data_ = SkData::MakeWithProc( pixel_lock, info.width * info.height * sizeof(uint32_t), on_release, - bitmap); + bitmap.release()); } bool AndroidImageGenerator::Register(JNIEnv* env) {