Perform cleanup operation on destroy()
In some cases, it's possible that the destructor of the
SensorEventConnection object is never called, because unless
the cleanupConnection() call is made, the SensorService still
has a strong reference to the SensorEventConnection. This may
result in leaked resources, because the connection still exists
in the SensorService, though the connection has been destroyed
from the client's perspective.
This CL fixes this issue by doing the cleanup operation in the
destroy() call instead.
Bug: 274595196
Test: Run STS test, and verify all connections are destroyed afterwards
Change-Id: I7edb72d17337420e19352874bcae1d36b5981b95
diff --git a/services/sensorservice/SensorEventConnection.cpp b/services/sensorservice/SensorEventConnection.cpp
index b94b1c0..7a6b31d 100644
--- a/services/sensorservice/SensorEventConnection.cpp
+++ b/services/sensorservice/SensorEventConnection.cpp
@@ -55,14 +55,13 @@
SensorService::SensorEventConnection::~SensorEventConnection() {
ALOGD_IF(DEBUG_CONNECTIONS, "~SensorEventConnection(%p)", this);
destroy();
- mService->cleanupConnection(this);
- if (mEventCache != nullptr) {
- delete[] mEventCache;
- }
+ delete[] mEventCache;
}
void SensorService::SensorEventConnection::destroy() {
- mDestroyed = true;
+ if (!mDestroyed.exchange(true)) {
+ mService->cleanupConnection(this);
+ }
}
void SensorService::SensorEventConnection::onFirstRef() {