forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[darwin-framework-tool] Automatically check for leaks on shutdown if …
…enable_leak_checking is true (project-chip#35936)
- Loading branch information
1 parent
6034c87
commit bd8803a
Showing
5 changed files
with
126 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright (c) 2024 Project CHIP Authors | ||
* All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
#pragma once | ||
|
||
/* | ||
* This function performs a memory leak check if the build flag `enable_leak_checking` is set to true | ||
* If leaks are detected, it overrides the provided exit code with `EXIT_FAILURE`. | ||
* | ||
* @param exitCode The initial exit code to return if no leaks are detected or if leak checking is disabled. | ||
* @return `EXIT_FAILURE` if leaks are detected and leak checking is enabled; otherwise, the original `exitCode`. | ||
*/ | ||
int ConditionalLeaksCheck(int exitCode); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright (c) 2024 Project CHIP Authors | ||
* All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
#include "LeakChecker.h" | ||
|
||
#import <Foundation/Foundation.h> | ||
#include <unistd.h> // For getpid() | ||
|
||
@interface LeakChecker : NSObject | ||
- (BOOL)hasMemoryLeaks; | ||
@end | ||
|
||
@implementation LeakChecker | ||
|
||
- (BOOL)hasMemoryLeaks | ||
{ | ||
pid_t pid = getpid(); | ||
auto * pidString = [NSString stringWithFormat:@"%d", pid]; | ||
|
||
auto * task = [[NSTask alloc] init]; | ||
task.launchPath = @"/usr/bin/leaks"; | ||
task.arguments = @[ pidString ]; | ||
|
||
auto * pipe = [NSPipe pipe]; | ||
task.standardOutput = pipe; | ||
task.standardError = pipe; | ||
|
||
NSFileHandle * fileHandle = [pipe fileHandleForReading]; | ||
[task launch]; | ||
[task waitUntilExit]; | ||
|
||
int exitCode = [task terminationStatus]; | ||
if (exitCode) { | ||
NSData * data = [fileHandle readDataToEndOfFile]; | ||
NSString * output = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; | ||
NSLog(@"%@", output); | ||
return YES; | ||
} | ||
|
||
return NO; | ||
} | ||
|
||
@end | ||
|
||
int ConditionalLeaksCheck(int exitCode) | ||
{ | ||
#ifdef DFT_ENABLE_LEAK_CHECKING | ||
auto * leakChecker = [[LeakChecker alloc] init]; | ||
if ([leakChecker hasMemoryLeaks]) { | ||
return EXIT_FAILURE; | ||
} | ||
#endif // DFT_ENABLE_LEAK_CHECKING | ||
|
||
return exitCode; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters