ThresholdTimer: use weak_ptr in async callback

Capture weak_ptr to sensor in ThresholdTimer async callback.
which is used to  detect destructed sensor in the callback.
Removed raw sensor pointer inside the ThresholdTimer.

When ADCSensor is destructed, ThresholdTimer is cancelled,
but already queued timer callback is not removed and can be
executed after the sensor is desctructed.

This change prevents accessing the dangling raw sensor pointer
and fixes the occasional ADCSensor service crash while trying to
run createSensors. Any services use ThresholdTimer have
the same issue.

Tested:
ipmitool power cycle 1000 times, ADCSensors get deleted and recreated
for every cycle without crashing.

Signed-off-by: AppaRao Puli <apparao.puli@linux.intel.com>
Signed-off-by: Zhikui Ren <zhikui.ren@intel.com>
Change-Id: Ibee7a58a2605992554fb33f4c34ebee502eb38d6
diff --git a/src/ADCSensor.cpp b/src/ADCSensor.cpp
index ef7a90a..e77261a 100644
--- a/src/ADCSensor.cpp
+++ b/src/ADCSensor.cpp
@@ -61,7 +61,7 @@
     inputDev(io, open(path.c_str(), O_RDONLY)), waitTimer(io), path(path),
     scaleFactor(scaleFactor),
     sensorPollMs(static_cast<unsigned int>(pollRate * 1000)),
-    bridgeGpio(std::move(bridgeGpio)), thresholdTimer(io, this)
+    bridgeGpio(std::move(bridgeGpio)), thresholdTimer(io)
 {
     sensorInterface = objectServer.add_interface(
         "/xyz/openbmc_project/sensors/voltage/" + name,
@@ -88,6 +88,7 @@
     // close the input dev to cancel async operations
     inputDev.close();
     waitTimer.cancel();
+
     objServer.remove_interface(thresholdInterfaceWarning);
     objServer.remove_interface(thresholdInterfaceCritical);
     objServer.remove_interface(sensorInterface);
@@ -230,5 +231,5 @@
         return;
     }
 
-    thresholds::checkThresholdsPowerDelay(this, thresholdTimer);
+    thresholds::checkThresholdsPowerDelay(weak_from_this(), thresholdTimer);
 }