diff --git a/src/darwin/Framework/CHIP/MTRDeviceControllerFactory.mm b/src/darwin/Framework/CHIP/MTRDeviceControllerFactory.mm index 085d0a2c55ca6d..23643d6a52fa18 100644 --- a/src/darwin/Framework/CHIP/MTRDeviceControllerFactory.mm +++ b/src/darwin/Framework/CHIP/MTRDeviceControllerFactory.mm @@ -73,8 +73,12 @@ static bool sExitHandlerRegistered = false; static void ShutdownOnExit() { - MTR_LOG("ShutdownOnExit invoked on exit"); - [[MTRDeviceControllerFactory sharedInstance] stopControllerFactory]; + // Depending on the structure of the software, this code might execute *after* the main autorelease pool has exited. + // Therefore, it needs to be enclosed in its own autorelease pool. + @autoreleasepool { + MTR_LOG("ShutdownOnExit invoked on exit"); + [[MTRDeviceControllerFactory sharedInstance] stopControllerFactory]; + } } @interface MTRDeviceControllerFactoryParams ()