[android] Fix leak of bitmap object (android.graphics.Bitmap of Java) while using platform image decoder. (flutter/engine#33953)

This commit is contained in:
toneyzeng 2022-06-18 03:22:05 +08:00 committed by GitHub
parent d1e9f3570d
commit df87bef96d

View File

@ -4,6 +4,8 @@
#include "flutter/shell/platform/android/android_image_generator.h"
#include <memory>
#include <android/bitmap.h>
#include <android/hardware_buffer.h>
@ -94,11 +96,10 @@ void AndroidImageGenerator::DoDecodeImage() {
env, env->NewDirectByteBuffer(const_cast<void*>(data_->data()),
data_->size()));
fml::jni::ScopedJavaGlobalRef<jobject>* bitmap =
new fml::jni::ScopedJavaGlobalRef(
env, env->CallStaticObjectMethod(
g_flutter_jni_class->obj(), g_decode_image_method,
direct_buffer.obj(), reinterpret_cast<long>(this)));
auto bitmap = std::make_unique<fml::jni::ScopedJavaGlobalRef<jobject>>(
env, env->CallStaticObjectMethod(
g_flutter_jni_class->obj(), g_decode_image_method,
direct_buffer.obj(), reinterpret_cast<long>(this)));
FML_CHECK(fml::jni::CheckException(env));
if (bitmap->is_null()) {
@ -127,11 +128,12 @@ void AndroidImageGenerator::DoDecodeImage() {
reinterpret_cast<fml::jni::ScopedJavaGlobalRef<jobject>*>(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) {