xml解析期间的内存泄漏问题?
我得到了内存泄漏的奇怪问题,xml解析期间的内存泄漏问题?
现在我的代码,
-(NSMutableDictionary *)getParsedWallpaperData{
NSMutableDictionary *dataDictionary = [NSMutableDictionary dictionary];
NSData *xmlData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Wallpaper" ofType:@"xml"]];
TBXML *tbXml = [[TBXML alloc] initWithXMLData:xmlData error:nil];
//TBXML *tbXml = [[TBXML tbxmlWithXMLData:xmlData error:nil] autorelease];
@synchronized(self){
TBXMLElement *rootXMLElement = tbXml.rootXMLElement;
if(rootXMLElement)
{
TBXMLElement *paging = [TBXML childElementNamed:kPaging parentElement:rootXMLElement];
NSMutableDictionary *pagingData = [[NSMutableDictionary alloc] init];
if(paging){
TBXMLElement *totalPages = [TBXML childElementNamed:kTotalPages parentElement:paging];
NSString *totalPagesString = [TBXML textForElement:totalPages];
[pagingData setObject:totalPagesString forKey:@"TotalPages"];
TBXMLElement *currentPage = [TBXML childElementNamed:kCurrentPage parentElement:paging];
NSString *currentPageString = [TBXML textForElement:currentPage];
[pagingData setObject:currentPageString forKey:@"CurrentPage"];
TBXMLElement *prevPage = [TBXML childElementNamed:kPerviousPage parentElement:paging];
NSString *prevPageString = [TBXML textForElement:prevPage];
[pagingData setObject:prevPageString forKey:@"PreviousPage"];
TBXMLElement *nextPage = [TBXML childElementNamed:kNextPage parentElement:paging];
NSString *nextPageString = [TBXML textForElement:nextPage];
[pagingData setObject:nextPageString forKey:@"NextPage"];
}
[dataDictionary setObject:pagingData forKey:@"PagingInfo"];
[pagingData release];
pagingData = nil;
TBXMLElement *totalItems = [TBXML childElementNamed:kTotalItems parentElement:rootXMLElement];
NSString *totalItemsString = [TBXML textForElement:totalItems];
[dataDictionary setObject:totalItemsString forKey:@"TotalItems"];
NSMutableArray *itemArray = [[NSMutableArray alloc] initWithCapacity:[totalItemsString intValue]];
TBXMLElement *items = [TBXML childElementNamed:kItems parentElement:rootXMLElement];
if(items){
TBXMLElement *item = [TBXML childElementNamed:kItem parentElement:items];
while (item) {
NSMutableDictionary *itemInfoDict = [[NSMutableDictionary alloc] init];
TBXMLElement *title = [TBXML childElementNamed:kTitle parentElement:item];
NSString *titleString = [TBXML textForElement:title];
[itemInfoDict setObject:titleString forKey:@"Title"];
TBXMLElement *image1 = [TBXML childElementNamed:kImage1 parentElement:item];
NSString *image1String = [TBXML textForElement:image1];
[itemInfoDict setObject:image1String forKey:@"Image1"];
TBXMLElement *image2 = [TBXML childElementNamed:kImage2 parentElement:item];
NSString *image2String = [TBXML textForElement:image2];
[itemInfoDict setObject:image2String forKey:@"Image2"];
TBXMLElement *image3 = [TBXML childElementNamed:kImage3 parentElement:item];
NSString *image3String = [TBXML textForElement:image3];
[itemInfoDict setObject:image3String forKey:@"Image3"];
TBXMLElement *thumbnail = [TBXML childElementNamed:kThumbnail parentElement:item];
NSString *thumbnailString = [TBXML textForElement:thumbnail];
[itemInfoDict setObject:thumbnailString forKey:@"Thumbnail"];
[itemArray addObject:itemInfoDict];
[itemInfoDict release];
itemInfoDict = nil;
item = item -> nextSibling;
}
}
[dataDictionary setObject:itemArray forKey:@"ImagesInfo"];
[itemArray release];
itemArray = nil;
}
}
[tbXml release];
tbXml = nil;
return dataDictionary;
}
我发现内存泄漏只TBXML * TBXML = [[TBXML页头] initWithXMLData:XMLDATA错误:零]在这一行上,即使我手动释放tbXml对象,
请给我建议y发生这种情况?
感谢,
那么,如果它的根元素有展示为漏水,我想知道,如果像childElementNamed
的访问者的一个原因造成的(通过返回的东西,就像一个的NSString但实际上也已经优化了存储回根元素的指针)。你可以看看childElementNamed
的执行情况吗?一种相对较快的方式来更改您的代码,以确保它不是将您要存储在dataDictionary
那里的任何NSString结果用[NSString stringWithFormat:@"%@", [TBXML textForElement:fooTitle]]
调用打包。
此外,如果TBXML正在创建大量的自动释放对象,则可以将此函数包装在一个@nsautoreleasepool
宏中。
作为最终建议,如果可以的话,您应该看看ARC(即,如果您正在部署到iOS 4+)。
我开始在ARC环境下编写代码,但是我的客户端改变了要求,他说我想给用户在应用程序中使用paly cocos2d游戏。并且该游戏代码与ARC不兼容,因此我选择非ARC应用程序... :( – Tirth 2012-03-19 11:02:58
)我在TBXML * tbXml = [[TBXML alloc] initWithXMLData:xmlData错误:无]中获得100%泄漏; 此行 – Tirth 2012-03-19 11:18:17
是的 - 但这并不意味着问题出现在TBXML中,某些东西引用了分配在该行上的内存,所以问题在其他地方 – 2012-03-19 11:53:15
您是否检查过TBXML库中是否存在泄漏? – freespace 2012-03-19 07:45:25
@freespace,我想在TBXML库中可能会泄漏,因为当我使用分析器调试我的代码时,它并没有给我任何问题,但是它在TBXML库中显示了很多问题...: ( – Tirth 2012-03-19 07:51:29
然后或者(a)向TBXML提交错误报告,或者(b)停止使用TBXML或(c)停止使用XML,我推荐使用选项(c) – freespace 2012-03-19 08:07:07