我有一个看起来像社交网络 iOS 应用的示例应用。我是可可框架的新手,所以我正在研究示例代码。点击分析后,应用程序中报告了 255 次内存泄漏。我能够解决大约 100 个非常简单的泄漏,但我无法解决其余的问题。
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
//DLog(@"found this element: %@", elementName);
currentElement = [elementName copy];
if([MethodName isEqualToString:@"SignInStep"])
{
if ([elementName isEqualToString:@"item"])
{ // clear out our story item caches...
item = [[NSMutableDictionary alloc] init];
currentUserId = [[NSMutableString alloc] init];
currentError = [[NSMutableString alloc] init];
}
}
}
项目的变量分配:
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
//DLog(@"ended element: %@", elementName);
if([MethodName isEqualToString:@"SignInStep"])
{
if ([elementName isEqualToString:@"item"])
{ // save values to an item, then store that item into the array...
[item setObject:currentUserId forKey:@"userId"];
[item setObject:currentError forKey:@"error"];
[SignIn addObject:[item copy]]; //Method returns Objective C Object with +1 retain count
}
}
}//Object leaked: allocated object is not referenced later in this execution path and has a retain count of +1
我收到以下错误:
1) 方法返回具有 +1 保留计数的 Objective C 对象
2) 对象泄露:分配的对象在此执行路径后面没有被引用,并且保留计数为+1
我在上面的代码中提到了我收到这些泄漏的确切位置。谁能告诉我这是什么原因造成的?