Skip to content

Commit e7600f2

Browse files
Darwin: MTRDeviceControllerFactory cleanup (cont)
Allocate C++ objects that are created at init time directly as ivars instead of as pointers. This avoids the need for manual cleanup code.
1 parent 56a1dfb commit e7600f2

File tree

3 files changed

+21
-88
lines changed

3 files changed

+21
-88
lines changed

src/darwin/Framework/CHIP/MTRDeviceController.mm

+1-1
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ - (BOOL)startup:(MTRDeviceControllerStartupParamsInternal *)startupParams
551551
}
552552

553553
errorCode = chip::Credentials::SetSingleIpkEpochKey(
554-
_factory.groupData, fabricIdx, _operationalCredentialsDelegate->GetIPK(), compressedId);
554+
_factory.groupDataProvider, fabricIdx, _operationalCredentialsDelegate->GetIPK(), compressedId);
555555
if ([self checkForStartError:errorCode logMsg:kErrorIPKInit]) {
556556
return;
557557
}

src/darwin/Framework/CHIP/MTRDeviceControllerFactory.mm

+19-86
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,9 @@
6969

7070
static NSString * const kErrorPersistentStorageInit = @"Init failure while creating a persistent storage delegate";
7171
static NSString * const kErrorSessionResumptionStorageInit = @"Init failure while creating a session resumption storage delegate";
72-
static NSString * const kErrorGroupProviderInit = @"Init failure while initializing group data provider";
73-
static NSString * const kErrorControllersInit = @"Init controllers array failure";
74-
static NSString * const kErrorCertificateValidityPolicyInit = @"Init certificate validity policy failure";
7572
static NSString * const kErrorControllerFactoryInit = @"Init failure while initializing controller factory";
7673
static NSString * const kErrorKeystoreInit = @"Init failure while initializing persistent storage keystore";
7774
static NSString * const kErrorCertStoreInit = @"Init failure while initializing persistent storage operational certificate store";
78-
static NSString * const kErrorSessionKeystoreInit = @"Init failure while initializing session keystore";
7975

8076
static bool sExitHandlerRegistered = false;
8177
static void ShutdownOnExit()
@@ -97,13 +93,13 @@ @implementation MTRDeviceControllerFactory {
9793
dispatch_queue_t _chipWorkQueue;
9894
DeviceControllerFactory * _controllerFactory;
9995

100-
Credentials::IgnoreCertificateValidityPeriodPolicy * _certificateValidityPolicy;
101-
Crypto::RawKeySessionKeystore * _sessionKeystore;
96+
Credentials::IgnoreCertificateValidityPeriodPolicy _certificateValidityPolicy;
97+
Crypto::RawKeySessionKeystore _sessionKeystore;
10298
// We use TestPersistentStorageDelegate just to get an in-memory store to back
10399
// our group data provider impl. We initialize this store correctly on every
104100
// controller startup, so don't need to actually persist it.
105-
TestPersistentStorageDelegate * _groupStorageDelegate;
106-
Credentials::GroupDataProviderImpl * _groupDataProvider;
101+
TestPersistentStorageDelegate _groupStorageDelegate;
102+
Credentials::GroupDataProviderImpl _groupDataProvider;
107103

108104
// _usingPerControllerStorage is only written once, during controller
109105
// factory start. After that it is only read, and can be read from
@@ -201,54 +197,30 @@ - (instancetype)init
201197
// cost to having an idle dispatch queue, and it simplifies our logic.
202198
DeviceLayer::PlatformMgrImpl().StartEventLoopTask();
203199

204-
_running = NO;
205200
_chipWorkQueue = DeviceLayer::PlatformMgrImpl().GetWorkQueue();
206201
_controllerFactory = &DeviceControllerFactory::GetInstance();
207-
_controllersLock = OS_UNFAIR_LOCK_INIT;
208-
209-
_sessionKeystore = new chip::Crypto::RawKeySessionKeystore();
210-
if ([self checkForInitError:(_sessionKeystore != nullptr) logMsg:kErrorSessionKeystoreInit]) {
211-
return nil;
212-
}
213-
214-
_groupStorageDelegate = new chip::TestPersistentStorageDelegate();
215-
if ([self checkForInitError:(_groupStorageDelegate != nullptr) logMsg:kErrorGroupProviderInit]) {
216-
return nil;
217-
}
218202

203+
// Initialize our default-constructed GroupDataProviderImpl.
219204
// For now default args are fine, since we are just using this for the IPK.
220-
_groupDataProvider = new chip::Credentials::GroupDataProviderImpl();
221-
if ([self checkForInitError:(_groupDataProvider != nullptr) logMsg:kErrorGroupProviderInit]) {
222-
return nil;
223-
}
224-
225-
_groupDataProvider->SetStorageDelegate(_groupStorageDelegate);
226-
_groupDataProvider->SetSessionKeystore(_sessionKeystore);
227-
CHIP_ERROR errorCode = _groupDataProvider->Init();
228-
if ([self checkForInitError:(CHIP_NO_ERROR == errorCode) logMsg:kErrorGroupProviderInit]) {
229-
return nil;
230-
}
205+
_groupDataProvider.SetStorageDelegate(&_groupStorageDelegate);
206+
_groupDataProvider.SetSessionKeystore(&_sessionKeystore);
207+
CHIP_ERROR err = _groupDataProvider.Init();
208+
VerifyOrDieWithMsg(err == CHIP_NO_ERROR, NotSpecified,
209+
"GroupDataProviderImpl::Init() failed: %" CHIP_ERROR_FORMAT, err.Format());
231210

211+
_controllersLock = OS_UNFAIR_LOCK_INIT;
232212
_controllers = [[NSMutableArray alloc] init];
233-
if ([self checkForInitError:(_controllers != nil) logMsg:kErrorControllersInit]) {
234-
return nil;
235-
}
236213

237-
_certificateValidityPolicy = new Credentials::IgnoreCertificateValidityPeriodPolicy();
238-
if ([self checkForInitError:(_certificateValidityPolicy != nil) logMsg:kErrorCertificateValidityPolicyInit]) {
239-
return nil;
240-
}
241-
242-
_serverEndpoints = [[NSMutableArray alloc] init];
243214
_serverEndpointsLock = OS_UNFAIR_LOCK_INIT;
215+
_serverEndpoints = [[NSMutableArray alloc] init];
244216

245217
return self;
246218
}
247219

248220
- (void)dealloc
249221
{
250222
[self stopControllerFactory];
251-
[self cleanupInitObjects];
223+
_groupDataProvider.Finish();
252224
}
253225

254226
- (void)_assertCurrentQueueIsNotMatterQueue
@@ -271,45 +243,6 @@ - (BOOL)checkIsRunning:(NSError * __autoreleasing *)error
271243
return NO;
272244
}
273245

274-
- (BOOL)checkForInitError:(BOOL)condition logMsg:(NSString *)logMsg
275-
{
276-
if (condition) {
277-
return NO;
278-
}
279-
280-
MTR_LOG_ERROR("Error: %@", logMsg);
281-
282-
[self cleanupInitObjects];
283-
284-
return YES;
285-
}
286-
287-
- (void)cleanupInitObjects
288-
{
289-
_controllers = nil;
290-
291-
if (_groupDataProvider) {
292-
_groupDataProvider->Finish();
293-
delete _groupDataProvider;
294-
_groupDataProvider = nullptr;
295-
}
296-
297-
if (_groupStorageDelegate) {
298-
delete _groupStorageDelegate;
299-
_groupStorageDelegate = nullptr;
300-
}
301-
302-
if (_sessionKeystore) {
303-
delete _sessionKeystore;
304-
_sessionKeystore = nullptr;
305-
}
306-
307-
if (_certificateValidityPolicy) {
308-
delete _certificateValidityPolicy;
309-
_certificateValidityPolicy = nullptr;
310-
}
311-
}
312-
313246
- (void)cleanupStartupObjects
314247
{
315248
assertChipStackLockedByCurrentThread();
@@ -485,12 +418,12 @@ - (BOOL)_startControllerFactory:(MTRDeviceControllerFactoryParams *)startupParam
485418
}
486419
params.enableServerInteractions = startupParams.shouldStartServer;
487420

488-
params.groupDataProvider = _groupDataProvider;
489-
params.sessionKeystore = _sessionKeystore;
421+
params.groupDataProvider = &_groupDataProvider;
422+
params.sessionKeystore = &_sessionKeystore;
490423
params.fabricIndependentStorage = _persistentStorageDelegate;
491424
params.operationalKeystore = _keystore;
492425
params.opCertStore = _opCertStore;
493-
params.certificateValidityPolicy = _certificateValidityPolicy;
426+
params.certificateValidityPolicy = &_certificateValidityPolicy;
494427
params.sessionResumptionStorage = _sessionResumptionStorage;
495428
errorCode = _controllerFactory->Init(params);
496429
if (errorCode != CHIP_NO_ERROR) {
@@ -1025,7 +958,7 @@ - (void)controllerShuttingDown:(MTRDeviceController *)controller
1025958
// Clear out out group keys for this fabric index, in case fabric
1026959
// indices get reused later. If a new controller is started on the
1027960
// same fabric it will be handed the IPK at that point.
1028-
self->_groupDataProvider->RemoveGroupKeys(fabricIndex);
961+
self->_groupDataProvider.RemoveGroupKeys(fabricIndex);
1029962
}
1030963

1031964
// If there are no other controllers left, we can shut down some things.
@@ -1269,9 +1202,9 @@ - (PersistentStorageDelegate *)storageDelegate
12691202
return _persistentStorageDelegate;
12701203
}
12711204

1272-
- (Credentials::GroupDataProvider *)groupData
1205+
- (Credentials::GroupDataProvider *)groupDataProvider
12731206
{
1274-
return _groupDataProvider;
1207+
return &_groupDataProvider;
12751208
}
12761209

12771210
@end

src/darwin/Framework/CHIP/MTRDeviceControllerFactory_Internal.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ MTR_DIRECT_MEMBERS
126126
- (nullable NSNumber *)neededReadPrivilegeForClusterID:(NSNumber *)clusterID attributeID:(NSNumber *)attributeID;
127127

128128
@property (readonly) chip::PersistentStorageDelegate * storageDelegate;
129-
@property (readonly) chip::Credentials::GroupDataProvider * groupData;
129+
@property (readonly) chip::Credentials::GroupDataProvider * groupDataProvider;
130130

131131
@end
132132

0 commit comments

Comments
 (0)