지난 몇 시간 동안 코드에서 메모리 누수를 찾으려고했습니다. 여기입니다 : 내가 코코아 메모리 관리의 몇 가지 기본 규칙을 따르려고했는데, 어떻게 든 여전히 누수가있을 것 같습니다코드에서 누출을 찾을 수 없습니다.
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
expression = [expression stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]]; // expression is an NSString object.
NSArray *arguments = [NSArray arrayWithObjects:expression, [@"~/Desktop/file.txt" stringByExpandingTildeInPath], @"-n", @"--line-number", nil];
NSPipe *outPipe = [[NSPipe alloc] init];
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/usr/bin/grep"];
[task setArguments:arguments];
[task setStandardOutput:outPipe];
[outPipe release];
[task launch];
NSData *data = [[outPipe fileHandleForReading] readDataToEndOfFile];
[task waitUntilExit];
[task release];
NSString *string = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding];
string = [string stringByReplacingOccurrencesOfString:@"\r" withString:@""];
int linesNum = 0;
NSMutableArray *possibleMatches = [[NSMutableArray alloc] init];
if ([string length] > 0) {
NSArray *lines = [string componentsSeparatedByString:@"\n"];
linesNum = [lines count];
for (int i = 0; i < [lines count]; i++) {
NSString *currentLine = [lines objectAtIndex:i];
NSArray *values = [currentLine componentsSeparatedByString:@"\t"];
if ([values count] == 20)
[possibleMatches addObject:currentLine];
}
}
[string release];
[pool release];
return [possibleMatches autorelease];
, 나는 새는 배열 믿습니다. 가능한 경우 큰 눈에 띄게됩니다. 큰 파일을 "~/Desktop/file.txt"로 사용하고 grep-ing 할 때 많은 결과를 나타내는 표현식으로 코드를 시도 할 수 있습니다.
내가 뭘 만들고있어?
도움 주셔서 감사합니다.
- Ry가편집 : 난 그냥 내 코드에서 누수를 찾기 위해 연타하다 정적 분석기를 사용하지만 아무것도 발견하지 않습니다. 그것은 단지 죽은 초기화를 찾았지만 그 내 누출에 대해 책임을지지 않습니다 ... 여기
따로 : 인덱스가 필요하지 않을 때마다 빠른 열거 형을 사용해야합니다 ('for (NSString * currentLine in lines)'). 코드가 깨끗하고 타이핑이 적으며 속도가 빠릅니다. –