Don't pass sp<this> in constructor.
Another good reason for not doing this is that
if the function you pass the sp<> into doesn't
keep the strong reference, the object will get
destructed while it's still being constructed!
In this particular case, this could happen if
servicemanager->registerForNotification() calls
fail.
Test: no crash when servicemanager call fails.
Change-Id: I36f52ecf95688aacac4722158746d118d27f99d7
diff --git a/transport/ServiceManagement.cpp b/transport/ServiceManagement.cpp
index 71129bf..04f7290 100644
--- a/transport/ServiceManagement.cpp
+++ b/transport/ServiceManagement.cpp
@@ -470,18 +470,21 @@
Waiter(const std::string& interface, const std::string& instanceName,
const sp<IServiceManager1_1>& sm) : mInterfaceName(interface),
mInstanceName(instanceName), mSm(sm) {
- Return<bool> ret = mSm->registerForNotifications(interface, instanceName, this);
+ }
+
+ void onFirstRef() override {
+ Return<bool> ret = mSm->registerForNotifications(mInterfaceName, mInstanceName, this);
if (!ret.isOk()) {
LOG(ERROR) << "Transport error, " << ret.description()
- << ", during notification registration for " << interface << "/"
- << instanceName << ".";
+ << ", during notification registration for " << mInterfaceName << "/"
+ << mInstanceName << ".";
return;
}
if (!ret) {
- LOG(ERROR) << "Could not register for notifications for " << interface << "/"
- << instanceName << ".";
+ LOG(ERROR) << "Could not register for notifications for " << mInterfaceName << "/"
+ << mInstanceName << ".";
return;
}