update_engine: Make SystemState pointer static only
The style guide does not allow a global object with non-trivial dtor. It
can cause hidden problems and it has caused issue this CL is
fixing (look at the attached bug). Instead of keeping the ownership of
the SystemState in global, we can keep the ownership in the high level
object DaemonChromeOS and keep a global static pointer to it so it can
easily be accessed by SystemState::Get().
BUG=b:174212887
TEST=cros_workon_make --board reef --test update_engine
TEST=cros deploy + stop update-engine -> The update_engine did not crash anymore.
Change-Id: I442f4220bfd8586c59fcdfd7d699776362143467
Reviewed-on: https://chromium-review.googlesource.com/c/aosp/platform/system/update_engine/+/2566875
Tested-by: Amin Hassani <ahassani@chromium.org>
Reviewed-by: Jae Hoon Kim <kimjae@chromium.org>
Commit-Queue: Amin Hassani <ahassani@chromium.org>
diff --git a/cros/fake_system_state.h b/cros/fake_system_state.h
index 7eb830c..da36306 100644
--- a/cros/fake_system_state.h
+++ b/cros/fake_system_state.h
@@ -17,6 +17,8 @@
#ifndef UPDATE_ENGINE_CROS_FAKE_SYSTEM_STATE_H_
#define UPDATE_ENGINE_CROS_FAKE_SYSTEM_STATE_H_
+#include <memory>
+
#include <base/logging.h>
#include <gmock/gmock.h>
#include <policy/mock_device_policy.h>
@@ -43,10 +45,14 @@
// OOBE is completed even when there's no such marker file, etc.
class FakeSystemState : public SystemState {
public:
- static void CreateInstance() { g_instance_.reset(new FakeSystemState()); }
+ static void CreateInstance() {
+ static std::unique_ptr<FakeSystemState> system_state;
+ system_state.reset(new FakeSystemState());
+ g_pointer_ = system_state.get();
+ }
static FakeSystemState* Get() {
- return reinterpret_cast<FakeSystemState*>(g_instance_.get());
+ return reinterpret_cast<FakeSystemState*>(g_pointer_);
}
// Base class overrides. All getters return the current implementation of