user571365
user571365

Reputation: 11

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 setObject:currentContact forKey:@"contact"];

  [rssArray addObject:[item copy]];

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

 }
 [rssArray release];
}

Upvotes: 0

Views: 224

Answers (1)

Nickolay Olshevsky
Nickolay Olshevsky

Reputation: 14160

[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.

Upvotes: 11

Related Questions