[Matter.framework] Wrap the MatterControllerFactory shutdown code that is runned with atexit into its own autorelease pool (#35878)

diff --git a/src/darwin/Framework/CHIP/MTRDeviceControllerFactory.mm b/src/darwin/Framework/CHIP/MTRDeviceControllerFactory.mm
index 085d0a2..23643d6 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 ()