2014-06-11 138 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