From e343546d3302e9ee79dcb6c878e58f662e5b58e7 Mon Sep 17 00:00:00 2001 From: Prabir Pradhan Date: Thu, 10 Nov 2022 21:22:39 +0000 Subject: [PATCH] Remove RefBase from NativeInputManager Since we only expect NativeInputManager to be called once for the lifetime of system_server, we create it using "new" and use the pointer as a leash without ever deallocating or destroying it. Bug: 258710091 Test: boot Change-Id: I7be01735b663607b6f4e30147cd4ff80c9d951c5 --- ...android_server_input_InputManagerService.cpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/services/core/jni/com_android_server_input_InputManagerService.cpp b/services/core/jni/com_android_server_input_InputManagerService.cpp index 0d872370dcdc1..dc440e5637772 100644 --- a/services/core/jni/com_android_server_input_InputManagerService.cpp +++ b/services/core/jni/com_android_server_input_InputManagerService.cpp @@ -260,10 +260,9 @@ static std::string getStringElementFromJavaArray(JNIEnv* env, jobjectArray array // --- NativeInputManager --- -class NativeInputManager : public virtual RefBase, - public virtual InputReaderPolicyInterface, - public virtual InputDispatcherPolicyInterface, - public virtual PointerControllerPolicyInterface { +class NativeInputManager : public virtual InputReaderPolicyInterface, + public virtual InputDispatcherPolicyInterface, + public virtual PointerControllerPolicyInterface { protected: virtual ~NativeInputManager(); @@ -1516,8 +1515,14 @@ static jlong nativeInit(JNIEnv* env, jclass /* clazz */, jobject serviceObj, return 0; } - NativeInputManager* im = new NativeInputManager(serviceObj, messageQueue->getLooper()); - im->incStrong(0); + static std::once_flag nativeInitialize; + NativeInputManager* im = nullptr; + std::call_once(nativeInitialize, [&]() { + // Create the NativeInputManager, which should not be destroyed or deallocated for the + // lifetime of the process. + im = new NativeInputManager(serviceObj, messageQueue->getLooper()); + }); + LOG_ALWAYS_FATAL_IF(im == nullptr, "NativeInputManager was already initialized."); return reinterpret_cast(im); }