[darwin-framework-tool] Add some autorelease pools to CHIPCommandBridge::Run to make the memory graph cleaner (#36046)
diff --git a/examples/darwin-framework-tool/commands/common/CHIPCommandBridge.mm b/examples/darwin-framework-tool/commands/common/CHIPCommandBridge.mm
index 8b9a5d3..d52c29a 100644
--- a/examples/darwin-framework-tool/commands/common/CHIPCommandBridge.mm
+++ b/examples/darwin-framework-tool/commands/common/CHIPCommandBridge.mm
@@ -40,31 +40,40 @@
CHIP_ERROR CHIPCommandBridge::Run()
{
- ChipLogProgress(chipTool, "Running Command");
- ReturnErrorOnFailure(MaybeSetUpStack());
- SetIdentity(mCommissionerName.HasValue() ? mCommissionerName.Value() : kIdentityAlpha);
+ // In interactive mode, we want to avoid memory accumulating in the main autorelease pool,
+ // so we clear it after each command.
+ @autoreleasepool {
+ ChipLogProgress(chipTool, "Running Command");
+ // Although the body of `Run` is within its own autorelease pool, this code block is further wrapped
+ // in an additional autorelease pool. This ensures that when the memory dump graph command is used directly,
+ // we can verify there’s no additional noise from the autorelease pools—a kind of sanity check.
+ @autoreleasepool {
+ ReturnErrorOnFailure(MaybeSetUpStack());
+ }
+ SetIdentity(mCommissionerName.HasValue() ? mCommissionerName.Value() : kIdentityAlpha);
- {
- std::lock_guard<std::mutex> lk(cvWaitingForResponseMutex);
- mWaitingForResponse = YES;
+ {
+ std::lock_guard<std::mutex> lk(cvWaitingForResponseMutex);
+ mWaitingForResponse = YES;
+ }
+
+ ReturnLogErrorOnFailure(RunCommand());
+
+ auto err = StartWaiting(GetWaitDuration());
+
+ bool deferCleanup = (IsInteractive() && DeferInteractiveCleanup());
+
+ Shutdown();
+
+ if (deferCleanup) {
+ sDeferredCleanups.insert(this);
+ } else {
+ Cleanup();
+ }
+ MaybeTearDownStack();
+
+ return err;
}
-
- ReturnLogErrorOnFailure(RunCommand());
-
- auto err = StartWaiting(GetWaitDuration());
-
- bool deferCleanup = (IsInteractive() && DeferInteractiveCleanup());
-
- Shutdown();
-
- if (deferCleanup) {
- sDeferredCleanups.insert(this);
- } else {
- Cleanup();
- }
- MaybeTearDownStack();
-
- return err;
}
CHIP_ERROR CHIPCommandBridge::GetPAACertsFromFolder(NSArray<NSData *> * __autoreleasing * paaCertsResult)