Check if the object is still valid in the timeout callback function
There is a chance that the service will crash
if the sensor threshold is changed.
The root cause is that the timer cancel() API cannot cancel
the already expired callback handlers.
When the callback handler is executed after objects destroyed,
all variables are invalid. Change the object to shared_ptr,
and use a weak_ptr to check if the object is destroyed
Tested:
Change the threshold, to check if there is any service crash log.
Signed-off-by: Yong Li <yong.b.li@linux.intel.com>
Change-Id: I03044e1071285c8dafa78f47e45b14e63aad66fe
diff --git a/include/HwmonTempSensor.hpp b/include/HwmonTempSensor.hpp
index e041085..5124260 100644
--- a/include/HwmonTempSensor.hpp
+++ b/include/HwmonTempSensor.hpp
@@ -7,7 +7,8 @@
#include <string>
#include <vector>
-class HwmonTempSensor : public Sensor
+class HwmonTempSensor : public Sensor,
+ public std::enable_shared_from_this<HwmonTempSensor>
{
public:
HwmonTempSensor(const std::string& path, const std::string& objectType,
@@ -18,6 +19,7 @@
const std::string& sensorConfiguration,
const PowerState powerState);
~HwmonTempSensor();
+ void setupRead(void);
private:
sdbusplus::asio::object_server& objServer;
@@ -27,7 +29,7 @@
std::string path;
PowerState readState;
size_t errCount;
- void setupRead(void);
+
void handleResponse(const boost::system::error_code& err);
void checkThresholds(void) override;
};