iphone - xml解析时内存泄漏问题?

标签 iphone objective-c ios ipad memory-leaks

我遇到了奇怪的内存泄漏问题,

现在我的代码是,

-(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 alloc] initWithXMLData:xmlData error:nil];在这一行,即使我手动释放 tbXml 对象,

请建议我发生这种情况吗?

谢谢,

最佳答案

好吧,如果那里的根元素显示为泄漏,我想知道是否像 childElementNamed 这样的访问器之一导致了它(通过返回类似于 NSString 的东西,但实际上也已优化指针存储回根元素)。你能看一下 childElementNamed 的实现吗?更改代码以确保它不会的一种相对快速的方法是使用 [NSString stringWithFormat:@"%@"包装您要存储在 dataDictionary 中的任何 NSString 结果, [TBXML textForElement:fooTitle]] 调用。

此外,如果 TBXML 创建大量自动释放对象,您可以将此函数包装在 @nsautoreleasepool 宏中。

作为最后的建议,如果可以的话,您应该查看 ARC(即,如果您要部署到 iOS 4+)。

关于iphone - xml解析时内存泄漏问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9766454/

相关文章:

ios - 屏幕比例/大小在 iOS 8 中是正确的,但在同一设备上的 iOS 9 中是错误的

ios - 从 json 获取数据后将数据加载到 UILabels 和 UITextFields

iphone - Apple禁止在选项卡上的选项卡上单击,直到满足条件为止

ios - 在哪里调用 setNeedsDisplay 来更新 CALayer?

ios - 如何将圆形动画应用于单元格?

使用 HTTP POST 和 GET 绑定(bind)的 iPhone WSDL 服务

iphone - 后台 MOC 的 "save:"无效(但返回 YES)

ios - 有没有办法使用 SwiftUI 将可选对象绑定(bind)到 Toggle/Slider

objective-c - iOS检测用户的移动

ios - 不使用最新的Xcode版本开发ios应用程序可以吗?