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/include/Thresholds.hpp b/include/Thresholds.hpp
index df55182..af63f72 100644
--- a/include/Thresholds.hpp
+++ b/include/Thresholds.hpp
@@ -59,8 +59,7 @@
 struct ThresholdTimer
 {
 
-    ThresholdTimer(boost::asio::io_service& ioService, Sensor* sensor) :
-        io(ioService), sensor(sensor)
+    ThresholdTimer(boost::asio::io_service& ioService) : io(ioService)
     {}
 
     bool hasActiveTimer(const Threshold& threshold, bool assert)
@@ -98,12 +97,12 @@
         }
     }
 
-    void startTimer(const Threshold& threshold, bool assert,
+    void startTimer(const std::weak_ptr<Sensor>& weakSensor,
+                    const Threshold& threshold, bool assert,
                     double assertValue);
 
     boost::asio::io_service& io;
     std::list<TimerPair> timers;
-    Sensor* sensor;
 };
 
 bool parseThresholdsFromConfig(
@@ -129,6 +128,7 @@
 void updateThresholds(Sensor* sensor);
 // returns false if a critical threshold has been crossed, true otherwise
 bool checkThresholds(Sensor* sensor);
-void checkThresholdsPowerDelay(Sensor* sensor, ThresholdTimer& thresholdTimer);
+void checkThresholdsPowerDelay(const std::weak_ptr<Sensor>& weakSensor,
+                               ThresholdTimer& thresholdTimer);
 
 } // namespace thresholds