Skip to content

Commit 72eab93

Browse files
Prevent unclean dealloc of SystemState (#33128)
* Prevent unclean dealloc of SystemState Per current documentation calling Shutdown while any controller is alive is already prohibited. Also rename the recently-introduced isShutdown method to isShutDown. * Add comments and remove the VerifyOrDie() Upon closer reading Shutdown() was already guaranteed to be called and ensures that the reference count is 0 at that time, but the documentation was wrong.
1 parent 742e65b commit 72eab93

3 files changed

+13
-5
lines changed

src/controller/CHIPDeviceControllerFactory.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ CHIP_ERROR DeviceControllerFactory::Init(FactoryInitParams params)
6868
mSessionResumptionStorage = params.sessionResumptionStorage;
6969
mEnableServerInteractions = params.enableServerInteractions;
7070

71+
// Initialize the system state. Note that it is left in a somewhat
72+
// special state where it is initialized, but has a ref count of 0.
7173
CHIP_ERROR err = InitSystemState(params);
7274

7375
return err;
@@ -76,7 +78,7 @@ CHIP_ERROR DeviceControllerFactory::Init(FactoryInitParams params)
7678
CHIP_ERROR DeviceControllerFactory::ReinitSystemStateIfNecessary()
7779
{
7880
VerifyOrReturnError(mSystemState != nullptr, CHIP_ERROR_INCORRECT_STATE);
79-
VerifyOrReturnError(mSystemState->IsShutdown(), CHIP_NO_ERROR);
81+
VerifyOrReturnError(mSystemState->IsShutDown(), CHIP_NO_ERROR);
8082

8183
FactoryInitParams params;
8284
params.systemLayer = mSystemState->SystemLayer();
@@ -404,6 +406,8 @@ void DeviceControllerFactory::Shutdown()
404406
{
405407
if (mSystemState != nullptr)
406408
{
409+
// ~DeviceControllerSystemState will call Shutdown(),
410+
// which in turn ensures that the reference count is 0.
407411
Platform::Delete(mSystemState);
408412
mSystemState = nullptr;
409413
}

src/controller/CHIPDeviceControllerFactory.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,9 @@ class DeviceControllerFactory
172172

173173
// Shuts down matter and frees the system state.
174174
//
175-
// Must not be called while any controllers are alive.
175+
// Must not be called while any controllers are alive, or while any calls
176+
// to RetainSystemState or EnsureAndRetainSystemState have not been balanced
177+
// by a call to ReleaseSystemState.
176178
void Shutdown();
177179

178180
CHIP_ERROR SetupController(SetupParams params, DeviceController & controller);
@@ -195,7 +197,8 @@ class DeviceControllerFactory
195197
// all device controllers have ceased to exist. To avoid that, this method has been
196198
// created to permit retention of the underlying system state.
197199
//
198-
// NB: The system state will still be freed in Shutdown() regardless of this call.
200+
// Calls to this method must be balanced by calling ReleaseSystemState before Shutdown.
201+
//
199202
void RetainSystemState();
200203

201204
//
@@ -210,6 +213,7 @@ class DeviceControllerFactory
210213
bool ReleaseSystemState();
211214

212215
// Like RetainSystemState(), but will re-initialize the system state first if necessary.
216+
// Calls to this method must be balanced by calling ReleaseSystemState before Shutdown.
213217
CHIP_ERROR EnsureAndRetainSystemState();
214218

215219
//

src/controller/CHIPDeviceControllerSystemState.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ class DeviceControllerSystemState
169169
{
170170
auto count = mRefCount++;
171171
VerifyOrDie(count < std::numeric_limits<decltype(count)>::max()); // overflow
172-
VerifyOrDie(!IsShutdown()); // avoid zombie
172+
VerifyOrDie(!IsShutDown()); // avoid zombie
173173
return this;
174174
};
175175

@@ -197,7 +197,7 @@ class DeviceControllerSystemState
197197
mGroupDataProvider != nullptr && mReportScheduler != nullptr && mTimerDelegate != nullptr &&
198198
mSessionKeystore != nullptr && mSessionResumptionStorage != nullptr && mBDXTransferServer != nullptr;
199199
};
200-
bool IsShutdown() { return mHaveShutDown; }
200+
bool IsShutDown() { return mHaveShutDown; }
201201

202202
System::Layer * SystemLayer() const { return mSystemLayer; };
203203
Inet::EndPointManager<Inet::TCPEndPoint> * TCPEndPointManager() const { return mTCPEndPointManager; };

0 commit comments

Comments
 (0)