I’m having troubles releasing objects.. To explain it better I have included my code below.
NSTask *task = [NSTask new];
NSTask *grep = [NSTask new];
NSPipe *pipe = [NSPipe new];
[task setStandardError: pipe];
[grep setStandardInput: pipe];
[pipe release];
pipe = [NSPipe new];
[grep setStandardOutput: pipe];
[task launch];
[grep launch];
NSString *string = [[[[[[NSString alloc] initWithData: [[[grep standardOutput] fileHandleForReading] readDataToEndOfFile] encoding: NSASCIIStringEncoding] autorelease] componentsSeparatedByString: @" "] objectAtIndex: 3] substringToIndex: 8];
NSMutableDictionary *dict = [NSMutableDictionary new];
[dict setObject: string forKey: @"myKey"];
[records addObject: dict];
[dict release];
[task release];
[grep release];
[pipe release];
How would I release the string and are there any other leaks? Also, if I remove everything from the array records with removeAllObjects, is everything released okay then too? The array should never be released and be available at all time, I’m just worrying about its objects.
Edit: The only leak pointed out had to do with the NSPipe and should be fixed in the code.
Thanks for any help!
Memory management in Objective-C has one fundamental rule:
Thus every call to
newin your code sample should be balanced with a call toreleaseorautorelease. TheNSArray, along with most other objects in the code, isn’t created with either, so it doesn’t need to be released. The[NSString alloc]is autoreleased, so it’s taken care of. Collections manage their own items, retaining and releasing them as necessary: when an item is inserted, it’s retained; when it’s removed, it’s released. Dictionary keys are copied rather than retained.Where you’ve got an unbalanced
new(and hence leak) is the firstNSPipeyou created. Release it before creating the pipe for grep’s standard output. Perhaps you simply left it out of the sample, but you’re also not setting any arguments for the grep task.