From 206552204a03b424414b6e1ea1651aad6ac068fe Mon Sep 17 00:00:00 2001 From: Kianoosh Karami Date: Fri, 4 Feb 2022 09:28:38 -0600 Subject: [PATCH] Introduce a Shutdown method to DeviceControllerFactory (#14723) The new method would the symmetrical API for DeviceControllerFactory's already existing `Init()` API. What is the root of the problem? The problem we intend to solve is that we'd like to use `Shutdown()` method to shut down and clean up any memory/functionality enabled by `Init()` as opposed to waiting for the DeviceControllerFactory desctructor, which only happens once the program shuts down due to the nature of how DeviceControllerFactory is instantiated. DeviceControllerFactory can only be instantiated through ::GetInstance(), this method constructs DeviceControllerFactory only once for the life of the program (the constructor is a private method). Once the Init() is invoked, the `mSystemState` is allocated as shown in the following: ``` mSystemState = chip::Platform::New(stateParams); ``` However, in DeviceControllerFactory's destructor, which currently is only invoked once the program shuts down, the deallocation of `mSystemState` is requested via `chip::Platform::Delete`. This can be problematic since the application will need to ensure `chip::Platform` memory stays initailized throughout the memory shutdown. Adding a shutdown methods allows the applications to have a symmetry between the initialization and shutdown sequence related to the DeviceControllerFactory --- src/controller/CHIPDeviceControllerFactory.cpp | 5 +++++ src/controller/CHIPDeviceControllerFactory.h | 1 + 2 files changed, 6 insertions(+) diff --git a/src/controller/CHIPDeviceControllerFactory.cpp b/src/controller/CHIPDeviceControllerFactory.cpp index a631ebdc5752fd..67f125270529fe 100644 --- a/src/controller/CHIPDeviceControllerFactory.cpp +++ b/src/controller/CHIPDeviceControllerFactory.cpp @@ -210,6 +210,11 @@ CHIP_ERROR DeviceControllerFactory::ServiceEvents() } DeviceControllerFactory::~DeviceControllerFactory() +{ + Shutdown(); +} + +void DeviceControllerFactory::Shutdown() { if (mSystemState != nullptr) { diff --git a/src/controller/CHIPDeviceControllerFactory.h b/src/controller/CHIPDeviceControllerFactory.h index eb9b668ce27999..68de0144909e2e 100644 --- a/src/controller/CHIPDeviceControllerFactory.h +++ b/src/controller/CHIPDeviceControllerFactory.h @@ -91,6 +91,7 @@ class DeviceControllerFactory } CHIP_ERROR Init(FactoryInitParams params); + void Shutdown(); CHIP_ERROR SetupController(SetupParams params, DeviceController & controller); CHIP_ERROR SetupCommissioner(SetupParams params, DeviceCommissioner & commissioner);