diff --git a/examples/darwin-framework-tool/commands/common/CHIPCommandBridge.mm b/examples/darwin-framework-tool/commands/common/CHIPCommandBridge.mm index 8b9a5d3e9f1eb2..d52c29a5b04c8a 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 lk(cvWaitingForResponseMutex); - mWaitingForResponse = YES; - } + { + std::lock_guard lk(cvWaitingForResponseMutex); + mWaitingForResponse = YES; + } - ReturnLogErrorOnFailure(RunCommand()); + ReturnLogErrorOnFailure(RunCommand()); - auto err = StartWaiting(GetWaitDuration()); + auto err = StartWaiting(GetWaitDuration()); - bool deferCleanup = (IsInteractive() && DeferInteractiveCleanup()); + bool deferCleanup = (IsInteractive() && DeferInteractiveCleanup()); - Shutdown(); + Shutdown(); - if (deferCleanup) { - sDeferredCleanups.insert(this); - } else { - Cleanup(); - } - MaybeTearDownStack(); + if (deferCleanup) { + sDeferredCleanups.insert(this); + } else { + Cleanup(); + } + MaybeTearDownStack(); - return err; + return err; + } } CHIP_ERROR CHIPCommandBridge::GetPAACertsFromFolder(NSArray * __autoreleasing * paaCertsResult)