Hi,
I have yet again a memory management problem. Here's my code:
The code is constantly allocating more and more memory, although it shouldn't. It's noticeable when you grep a large file for an expression that's contained often in that file (e.g. if grep returns something like 400 lines).
I've looked with the Leaks tool from Instruments and the Clang Static Analyzer, but both do not find any leaks.
I really tried to adhere to the simple Cocoa memory management rules, but it seems like I'm doing something wrong.
Thanks for any help!
-ranguvar
I have yet again a memory management problem. Here's my code:
Code:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// expression is an NSString object.
expression = [expression stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
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 terminate];
[task release];
NSString *stringOrig = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding];
NSString *string = [stringOrig stringByReplacingOccurrencesOfString:@"\r" withString:@""];
int linesNum = 0;
NSMutableArray *possibleMatches = [[NSMutableArray alloc] init];
// Check if the output appears to be valid.
if ([string length] > 0) {
// Separate the different lines.
NSArray *lines = [string componentsSeparatedByString:@"\n"];
linesNum = [lines count];
// Now go through all the different lines.
for (int i = 0; i < [lines count]; i++) {
NSString *currentLine = [lines objectAtIndex:i];
NSArray *values = [currentLine componentsSeparatedByString:@"\t"];
// Let's check if this is a valid line.
if ([values count] == 20) {
// OK, this might be something we're looking for. Add it to the array for later inspection.
[possibleMatches addObject:currentLine];
}
}
}
[stringOrig release];
[pool release];
return [possibleMatches autorelease];
The code is constantly allocating more and more memory, although it shouldn't. It's noticeable when you grep a large file for an expression that's contained often in that file (e.g. if grep returns something like 400 lines).
I've looked with the Leaks tool from Instruments and the Clang Static Analyzer, but both do not find any leaks.
I really tried to adhere to the simple Cocoa memory management rules, but it seems like I'm doing something wrong.
Thanks for any help!
-ranguvar