I am trying to parse a an xml file. I am creating an array of dictionaries and then with these created arrays I am creating array with arrays. I am having memory leaks when I am copying my nsdictionary into array. can any one please help!! Thanks
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
if(parseMode == 1){
// NSLog(@"found this start tag: %@", elementName);
if ([elementName isEqualToString:@"Group"]) {
[tInState removeAllObjects];
}
else if ([elementName isEqualToString:@"State_Name"]) {
tData = [[NSMutableDictionary alloc] init];
xmlItem = 0;
}
else if ([elementName isEqualToString:@"T_Name"]) {
xmlItem = 1;
}
else if ([elementName isEqualToString:@"T_Address"]) {
xmlItem = 2;
}
else if ([elementName isEqualToString:@"T_Ph"]) {
xmlItem = 3;
}
}
}
- (void)parser:(NSXMLParser *)parser didEndE开发者_开发知识库lement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if (parseMode == 1) {
// NSLog(@"found this end tag: %@", elementName);
if([elementName isEqualToString:@"T_Info"]) {
[tInState addObject:[tData copy]];
[tData autorelease];
}
else if ([elementName isEqualToString:@"Group"]) {
[tlist addObject:tInState];
}
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if (parseMode == 0) {
[self addToStateList:string];
}
else if (parseMode == 1) {
// NSLog(@"found this character: %@", string);
switch (xmlItem) {
case 0:
[tData setValue:string forKey:@"State_Name"];
break;
case 1:
[tData setValue:string forKey:@"T_Name"];
break;
case 2:
[tData setValue:string forKey:@"T_Address"];
break;
case 3:
[tData setValue:string forKey:@"T_Ph"];
break;
default:
break;
}
}
}
Here tInState, tInState are nsmutablearray which I allocated in viewdidload function and released in releaseMemory. I went through the NScopy documents and it says when we use copy the ownership is transferred. I am releasing all my arrays so why is it still causing the problem??
Please help with this. Thanks
The reason for your leak is because tData is doing what you told it, returning a copy. Calling autorelease on tData on the next line is autoreleasing the original tData and that will result in an over release. You will need to change that line to this [tInState addObject:[[tData copy] autorelease]];
and remove the autorelease call on the next line.
On line 81 you are creating a copy of tData but not releasing it so it is leaking. Instead of creating the copy in the addObject
call, assign it to a variable, then pass it to addObject
, then release it.
id temp = [tData copy];
[tInState addObject:temp];
[temp release];
Also, I'm assuming that the [tData autorelease]
is actually meant to release the copy. If so, that's not necessary.
精彩评论