Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 23758d57 authored by Arpit Singh's avatar Arpit Singh Committed by Android (Google) Code Review
Browse files

Merge "Refactor native MotionEvent_obtain to avoid possible memory leak" into main

parents 879dae1c ccf45996
Loading
Loading
Loading
Loading
+4 −17
Original line number Diff line number Diff line
@@ -85,24 +85,9 @@ static void android_view_MotionEvent_setNativePtr(JNIEnv* env, ScopedLocalRef<jo

ScopedLocalRef<jobject> android_view_MotionEvent_obtainAsCopy(JNIEnv* env,
                                                              const MotionEvent& event) {
    ScopedLocalRef<jobject> eventObj(env,
                                     env->CallStaticObjectMethod(gMotionEventClassInfo.clazz,
                                                                 gMotionEventClassInfo.obtain));
    if (env->ExceptionCheck() || !eventObj.get()) {
        ALOGE("An exception occurred while obtaining a motion event.");
        LOGE_EX(env);
        env->ExceptionClear();
        return ScopedLocalRef<jobject>(env);
    }

    MotionEvent* destEvent = android_view_MotionEvent_getNativePtr(env, eventObj.get());
    if (!destEvent) {
        destEvent = new MotionEvent();
        android_view_MotionEvent_setNativePtr(env, eventObj, destEvent);
    }

    std::unique_ptr<MotionEvent> destEvent = std::make_unique<MotionEvent>();
    destEvent->copyFrom(&event, true);
    return eventObj;
    return android_view_MotionEvent_obtainFromNative(env, std::move(destEvent));
}

ScopedLocalRef<jobject> android_view_MotionEvent_obtainFromNative(
@@ -117,6 +102,8 @@ ScopedLocalRef<jobject> android_view_MotionEvent_obtainFromNative(
        LOGE_EX(env);
        LOG_ALWAYS_FATAL("An exception occurred while obtaining a Java motion event.");
    }
    MotionEvent* oldEvent = android_view_MotionEvent_getNativePtr(env, eventObj.get());
    delete oldEvent;
    android_view_MotionEvent_setNativePtr(env, eventObj, event.release());
    return eventObj;
}