2014-06-11 137 views
0

你好,我有這個功能,從文件中獲取項目虛擬數據: 問題顯示在這些線路:潛在泄漏

NSString *path = [[NSBundle mainBundle] pathForResource: @"StatisticsDataJSON" ofType: @"TXT"]; - 對象的潛在泄漏。

NSMutableDictionary *statisticsResponse = [jsonParser objectWithString:data]; - 的潛在泄漏 - 存儲到 'statisticArray'

for (int i = 0; i < statsForDate.count; i++) {物體的潛在的泄漏 - 存儲到 'jsonParser'

for (id key in statisticsResponse) {物體的潛在的泄漏存儲爲's'的對象

if (self.statistics==nil) 
{ 
    self.statistics = [[NSMutableDictionary alloc]init]; 
    NSString *path = [[NSBundle mainBundle] pathForResource: @"StatisticsDataJSON" ofType: @"TXT"]; 
    NSError *error = nil; 
    NSString *data = [NSString stringWithContentsOfFile: path 
              encoding: NSUTF8StringEncoding 
               error: &error]; 
    //NSLog(@"%@",data); 

    SBJsonParser *jsonParser = [[SBJsonParser alloc] init]; 
    NSMutableDictionary *statisticsResponse = [jsonParser objectWithString:data]; 

    for (id key in statisticsResponse) { 
     NSArray *statsForDate = [statisticsResponse objectForKey:key]; 
     NSMutableArray *statisticArray = [[NSMutableArray alloc]init]; 
     for (int i = 0; i < statsForDate.count; i++) { 
      Statistic *s = [[Statistic alloc]init]; 
      s.locationId = [[statsForDate objectAtIndex:i] objectForKey:@"locationId"]; 
      int value =[[[statsForDate objectAtIndex:i] objectForKey:@"visits"] integerValue]; 
      s.visits = value; 
      value =[[[statsForDate objectAtIndex:i] objectForKey:@"totalUsers"] integerValue]; 
      s.totalUsers = value; 
      value= [[[statsForDate objectAtIndex:i] objectForKey:@"uploads"] integerValue]; 
      s.uploads = value; 
      value = [[[statsForDate objectAtIndex:i] objectForKey:@"downloads"] integerValue]; 
      s.downloads = value; 
      value = [[[statsForDate objectAtIndex:i] objectForKey:@"apps"] integerValue]; 
      s.apps = value; 
      [statisticArray addObject:s]; 
     } 
     [self.statistics setObject:statisticArray forKey:key]; 
    }; 
} 

我發現在ststisticsResponse是自動釋放 - 解決問題:

NSMutableDictionary *statisticsResponse = [[jsonParser objectWithString:data]autorelease]; 

但後來東西SBJsonStreamParserAccumulator.m未能在dealoc功能。

什麼問題?

+0

你不使用ARC嗎? – borrrden

+0

舊項目 - 不是ARC – Cheese

+1

我建議您更新它。從它的外觀來看,寫它的人不知道內存管理。無論是那個還是你都沒有展示全貌。所有你分配/ init你需要稍後發佈。你在做那個嗎? – borrrden

回答

0

注意潛在泄漏的警告來吧的潛在泄漏後的行,因爲這是在其引用的對象在技術上「泄露」的第一點。所以你當前的修復可能是過度釋放並導致崩潰。

在你的問題的第一條語句實際上指的是泄漏在這一行,之前:

self.statistics = [[NSMutableDictionary alloc]init]; 

你有沒有進一步提及的是分配的字典,這是一個保留的財產,所以你必須泄漏。

self.statistics = [[[NSMutableDictionary alloc]init] autorelease]; 

會解決這個問題。下一個,你必須釋放jsonParser當你用完後(解析完成後):

[jsonParser release]; 

我不會去通過所有的人,但你應該得到的理念。基本上你需要閱讀內存管理指南,或更新到ARC。

注意警告中的變量名稱。他們告訴你泄漏的地方在哪裏。

+0

謝謝,在我看到你的回答之前,我處理了除self.statistics之外的所有泄漏。你的回答解決了我的最後一個問題=) – Cheese