From 8e1ab70ddaa965cb71d8158fd306271611718e96 Mon Sep 17 00:00:00 2001 From: Vivien Nicolas Date: Fri, 11 Oct 2024 21:57:20 +0200 Subject: [PATCH] [darwin-framework-tool] Add some autorelease pools to CHIPCommandBridge::Run to make the memory graph cleaner (#36046) --- .../commands/common/CHIPCommandBridge.mm | 45 +++++++++++-------- 1 file changed, 27 insertions(+), 18 deletions(-) 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)