我想解析一個xml文件。我正在創建一個字典數組,然後用這些創建的數組創建數組。當我將nsdictionary複製到數組中時,發生內存泄漏。可以任何人請幫助! 感謝解析數據時內存泄露
- (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 didEndElement:(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;
}
}
}
這裏tInState,tInState是nsmutab學習我在viewdidload函數中分配的內容並在releaseMemory中發佈。我瀏覽了NScopy文檔,它說當我們使用副本時,所有權被轉移。我釋放我的所有數組,爲什麼它仍然導致問題?
請幫忙。 謝謝
我錯過了清除didendelement中的行我不釋放nsdictionary(tdata)。如果我釋放它剛剛崩潰。 – pa12
請粘貼代碼並擺脫屏幕截圖(難以閱讀) –
考慮轉換爲ARC,這些問題會消失。 – Cthutu