开发者

Understanding reference counting with Objective C / Leak

I'm new with programming for the iPhone. I wrote a little App. The Part of the App is running fine in the simulator . I do not understand the following:

The Analyzer is not agree with the code. "Method returns an Objective-C object with a +1 retain count (owning reference)" he said.

Is anybody help me.

Thanks...

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
 if ([elementName isEqualToString:@"item"]) {
  [item setObject:currentTitle forKey:@"name"];
  [item setObject:currentAutor forKey:@"descr"];
  [item setObje开发者_运维技巧ct:currentContact forKey:@"contact"];

  [rssArray addObject:[item copy]];

  NSLog(@"Nachricht: %@", currentTitle);
  [item release];

 }
 [rssArray release];
}


[rssArray addObject:[item copy]];

This line produces memory leak, since rssArray sends retain to [item copy], and [item copy] creates new object, object [item copy] will have retainCount = 2, but will be released only once. You should use [[item copy] autorelease], or assign [item copy] to temporary variable and release it after it is added to array.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜