Fix objects that are derived from refbase

Fixes: ag/14313466
Bug: 177493042
Bug: 191163855
Test: Build
Change-Id: I94812997a8214b77a2e1d0bcf90ef62205c5adf6
Signed-off-by: Wei Wang <wvw@google.com>
diff --git a/aidl/power-libperfmgr/Power.cpp b/aidl/power-libperfmgr/Power.cpp
index 7fb113e..f1f3db9 100644
--- a/aidl/power-libperfmgr/Power.cpp
+++ b/aidl/power-libperfmgr/Power.cpp
@@ -90,7 +90,7 @@
 ndk::ScopedAStatus Power::setMode(Mode type, bool enabled) {
     LOG(DEBUG) << "Power setMode: " << toString(type) << " to: " << enabled;
     ATRACE_INT(toString(type).c_str(), enabled);
-    PowerSessionManager::getInstance().updateHintMode(toString(type), enabled);
+    PowerSessionManager::getInstance()->updateHintMode(toString(type), enabled);
 #ifdef MODE_EXT
     if (setDeviceSpecificMode(type, enabled)) {
         return ndk::ScopedAStatus::ok();
diff --git a/aidl/power-libperfmgr/PowerExt.cpp b/aidl/power-libperfmgr/PowerExt.cpp
index c3c2252..58dfd2e 100644
--- a/aidl/power-libperfmgr/PowerExt.cpp
+++ b/aidl/power-libperfmgr/PowerExt.cpp
@@ -47,7 +47,7 @@
     } else {
         mHintManager->EndHint(mode);
     }
-    PowerSessionManager::getInstance().updateHintMode(mode, enabled);
+    PowerSessionManager::getInstance()->updateHintMode(mode, enabled);
 
     return ndk::ScopedAStatus::ok();
 }
diff --git a/aidl/power-libperfmgr/PowerHintSession.cpp b/aidl/power-libperfmgr/PowerHintSession.cpp
index 908983f..be27209 100644
--- a/aidl/power-libperfmgr/PowerHintSession.cpp
+++ b/aidl/power-libperfmgr/PowerHintSession.cpp
@@ -146,7 +146,7 @@
     mDescriptor = new AppHintDesc(tgid, uid, threadIds, sUclampCap);
     mDescriptor->duration = std::chrono::nanoseconds(durationNanos);
     mStaleHandler = sp<StaleHandler>(new StaleHandler(this, sStaleTimeoutMs));
-    mPowerManagerHandler = sp<MessageHandler>(&PowerSessionManager::getInstance());
+    mPowerManagerHandler = PowerSessionManager::getInstance();
 
     if (ATRACE_ENABLED()) {
         std::string sz =
@@ -157,7 +157,7 @@
                           mDescriptor->uid, reinterpret_cast<uintptr_t>(this) & 0xffff);
         ATRACE_INT(sz.c_str(), mDescriptor->is_active.load());
     }
-    PowerSessionManager::getInstance().addPowerSession(this);
+    PowerSessionManager::getInstance()->addPowerSession(this);
     ALOGD("PowerHintSession created: %s", mDescriptor->toString().c_str());
 }
 
@@ -180,7 +180,7 @@
 }
 
 void PowerHintSession::updateUniveralBoostMode() {
-    PowerHintMonitor::getInstance().getLooper()->sendMessage(mPowerManagerHandler, NULL);
+    PowerHintMonitor::getInstance()->getLooper()->sendMessage(mPowerManagerHandler, NULL);
 }
 
 int PowerHintSession::setUclamp(int32_t min, int32_t max) {
@@ -247,10 +247,10 @@
 }
 
 ndk::ScopedAStatus PowerHintSession::close() {
-    PowerHintMonitor::getInstance().getLooper()->removeMessages(mStaleHandler);
+    PowerHintMonitor::getInstance()->getLooper()->removeMessages(mStaleHandler);
     // Reset to (0, 1024) uclamp value -- instead of threads' original setting.
     setUclamp(0, 1024);
-    PowerSessionManager::getInstance().removePowerSession(this);
+    PowerSessionManager::getInstance()->removePowerSession(this);
     updateUniveralBoostMode();
     return ndk::ScopedAStatus::ok();
 }
@@ -291,7 +291,7 @@
         ALOGE("Error: shouldn't report duration during pause state.");
         return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
     }
-    if (PowerHintMonitor::getInstance().isRunning() && isStale()) {
+    if (PowerHintMonitor::getInstance()->isRunning() && isStale()) {
         if (ATRACE_ENABLED()) {
             std::string sz = StringPrintf("adpf.%" PRId32 "-%" PRId32 "-%" PRIxPTR "-stale",
                                           mDescriptor->tgid, mDescriptor->uid,
@@ -401,7 +401,7 @@
 
 void PowerHintSession::StaleHandler::updateStaleTimer() {
     std::lock_guard<std::mutex> guard(mStaleLock);
-    if (PowerHintMonitor::getInstance().isRunning()) {
+    if (PowerHintMonitor::getInstance()->isRunning()) {
         auto when = getStaleTime();
         auto now = std::chrono::steady_clock::now();
         mLastUpdatedTime.store(now);
@@ -410,7 +410,7 @@
         }
         if (!mIsMonitoringStale.load()) {
             auto next = getStaleTime();
-            PowerHintMonitor::getInstance().getLooper()->sendMessageDelayed(
+            PowerHintMonitor::getInstance()->getLooper()->sendMessageDelayed(
                     duration_cast<nanoseconds>(next - now).count(), this, NULL);
             mIsMonitoringStale.store(true);
         }
@@ -432,7 +432,7 @@
         return;
     }
     // Schedule for the next checking time.
-    PowerHintMonitor::getInstance().getLooper()->sendMessageDelayed(
+    PowerHintMonitor::getInstance()->getLooper()->sendMessageDelayed(
             duration_cast<nanoseconds>(when - now).count(), this, NULL);
 }
 
diff --git a/aidl/power-libperfmgr/PowerSessionManager.cpp b/aidl/power-libperfmgr/PowerSessionManager.cpp
index f3cb29d..79dea6e 100644
--- a/aidl/power-libperfmgr/PowerSessionManager.cpp
+++ b/aidl/power-libperfmgr/PowerSessionManager.cpp
@@ -116,7 +116,7 @@
     return true;
 }
 
-Looper *PowerHintMonitor::getLooper() {
+sp<Looper> PowerHintMonitor::getLooper() {
     return mLooper;
 }
 
diff --git a/aidl/power-libperfmgr/PowerSessionManager.h b/aidl/power-libperfmgr/PowerSessionManager.h
index 992e876..006a565 100644
--- a/aidl/power-libperfmgr/PowerSessionManager.h
+++ b/aidl/power-libperfmgr/PowerSessionManager.h
@@ -53,8 +53,8 @@
     void setHintManager(std::shared_ptr<HintManager> const &hint_manager);
 
     // Singleton
-    static PowerSessionManager &getInstance() {
-        static PowerSessionManager instance;
+    static sp<PowerSessionManager> getInstance() {
+        static sp<PowerSessionManager> instance = new PowerSessionManager();
         return instance;
     }
 
@@ -80,17 +80,17 @@
   public:
     void start();
     bool threadLoop() override;
-    Looper *getLooper();
+    sp<Looper> getLooper();
     // Singleton
-    static PowerHintMonitor &getInstance() {
-        static PowerHintMonitor instance;
+    static sp<PowerHintMonitor> getInstance() {
+        static sp<PowerHintMonitor> instance = new PowerHintMonitor();
         return instance;
     }
     PowerHintMonitor(PowerHintMonitor const &) = delete;
     void operator=(PowerHintMonitor const &) = delete;
 
   private:
-    Looper *mLooper;
+    sp<Looper> mLooper;
     // Singleton
     PowerHintMonitor() : Thread(false), mLooper(new Looper(true)) {}
 };
diff --git a/aidl/power-libperfmgr/service.cpp b/aidl/power-libperfmgr/service.cpp
index 09a8429..bc10e0e 100644
--- a/aidl/power-libperfmgr/service.cpp
+++ b/aidl/power-libperfmgr/service.cpp
@@ -69,8 +69,8 @@
     LOG(INFO) << "Xiaomi Power HAL AIDL Service with Extension is started.";
 
     if (::android::base::GetIntProperty("vendor.powerhal.adpf.rate", -1) != -1) {
-        PowerHintMonitor::getInstance().start();
-        PowerSessionManager::getInstance().setHintManager(hm);
+        PowerHintMonitor::getInstance()->start();
+        PowerSessionManager::getInstance()->setHintManager(hm);
     }
 
     std::thread initThread([&]() {