2012-03-19 70 views
0

我得到了內存泄漏的奇怪問題,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發生這種情況?

感謝,

+0

您是否檢查過TBXML庫中是否存在泄漏? – freespace 2012-03-19 07:45:25

+0

@freespace,我想在TBXML庫中可能會泄漏,因爲當我使用分析器調試我的代碼時,它並沒有給我任何問題,但是它在TBXML庫中顯示了很多問題...: ( – Tirth 2012-03-19 07:51:29

+1

然後或者(a)向TBXML提交錯誤報告,或者(b)停止使用TBXML或(c)停止使用XML,我推薦使用選項(c) – freespace 2012-03-19 08:07:07

回答

1

那麼,如果它的根元素有展示爲漏水,我想知道,如果像childElementNamed的訪問者的一個原因造成的(通過返回的東西,就像一個的NSString但實際上也已經優化了存儲回根元素的指針)。你可以看看childElementNamed的執行情況嗎?一種相對較快的方式來更改您的代碼,以確保它不是將您要存儲在dataDictionary那裏的任何NSString結果用[NSString stringWithFormat:@"%@", [TBXML textForElement:fooTitle]]調用打包。

此外,如果TBXML正在創建大量的自動釋放對象,則可以將此函數包裝在一個@nsautoreleasepool宏中。

作爲最終建議,如果可以的話,您應該看看ARC(即,如果您正在部署到iOS 4+)。

+0

我開始在ARC環境下編寫代碼,但是我的客戶端改變了要求,他說我想給用戶在應用程序中使用paly cocos2d遊戲。並且該遊戲代碼與ARC不兼容,因此我選擇非ARC應用程序... :( – Tirth 2012-03-19 11:02:58

+0

)我在TBXML * tbXml = [[TBXML alloc] initWithXMLData:xmlData錯誤:無]中獲得100%泄漏; 此行 – Tirth 2012-03-19 11:18:17

+0

是的 - 但這並不意味着問題出現在TBXML中,某些東西引用了分配在該行上的內存,所以問題在其他地方 – 2012-03-19 11:53:15

0

在你的函數的頂部添加此行:

NSAutoreleasePool *Pool=[[NSAutoreleasePool alloc]init]; 

,並在你的函數的底部之前加入這一行「return」語句:

[Pool drain]; 

也許這將幫助你。

+0

它不適合我。 – Tirth 2012-03-19 11:17:09

+0

更換[Pool drain]; [Pool release];線。 – Kuldeep 2012-03-19 11:36:05

+0

它也不適用於我:( – Tirth 2012-03-19 12:16:12