【发布时间】:2010-03-09 18:29:42
【问题描述】:
我在释放对象时遇到了麻烦。为了更好地解释它,我在下面包含了我的代码。
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];
我将如何释放字符串,是否还有其他泄漏?另外,如果我使用removeAllObjects 从数组records 中删除所有内容,那么所有发布的内容都还好吗?数组永远不应该被释放并且始终可用,我只是担心它的对象。
编辑:指出的唯一泄漏与 NSPipe 有关,应在代码中修复。
感谢您的帮助!
【问题讨论】:
-
好的,您分配
string的代码简直是疯了。将其分解为更小的线条和泄漏等变得更加明显。