nvidia-gpu: Fix a number of object lifetime issues

Moves all subsensors and objects treated as shared_ptrs
to be using shared_from_this. This way, if there's an object lifetime
issue we don't segfault.

Also separates construction and asio init for NvidiaSmaDevice
so that when we bind to this, its valid after we leave the ctor

Change-Id: I8e3115bc276d2e0eaac0b1dc9a9d2c46e6751d4b
Signed-off-by: Marc Olberding <molberding@nvidia.com>
diff --git a/src/nvidia-gpu/NvidiaSmaDevice.cpp b/src/nvidia-gpu/NvidiaSmaDevice.cpp
index 755e1f8..71d0f69 100644
--- a/src/nvidia-gpu/NvidiaSmaDevice.cpp
+++ b/src/nvidia-gpu/NvidiaSmaDevice.cpp
@@ -35,6 +35,9 @@
     waitTimer(io, std::chrono::steady_clock::duration(0)),
     mctpRequester(mctpRequester), conn(conn), objectServer(objectServer),
     configs(configs), name(escapeName(name)), path(path)
+{}
+
+void SmaDevice::init()
 {
     makeSensors();
 }
@@ -56,11 +59,18 @@
     tempSensor->update();
 
     waitTimer.expires_after(std::chrono::milliseconds(sensorPollMs));
-    waitTimer.async_wait([this](const boost::system::error_code& ec) {
-        if (ec)
-        {
-            return;
-        }
-        read();
-    });
+    waitTimer.async_wait(
+        [weak{weak_from_this()}](const boost::system::error_code& ec) {
+            std::shared_ptr<SmaDevice> self = weak.lock();
+            if (!self)
+            {
+                lg2::error("Invalid SmaDevice reference");
+                return;
+            }
+            if (ec)
+            {
+                return;
+            }
+            self->read();
+        });
 }