2013-02-13 173 views
0

這裏是我的代碼:內存不釋放

-(void) encodeStationsBack 
{ 
    if (context == nil) 
    { 
     context = [(radioAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; 
     NSLog(@"After managedObjectContext: %@", context); 
    } 

    // here is JSON parsing 
    int i=0; 
    int count = stations.count; 

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Station" inManagedObjectContext:context]; 

    NSMutableArray* parsedData = [[NSMutableArray alloc] init]; 

    while(stations.count > 0) { 
     NSString*string = [[NSString alloc] initWithString:[stations objectAtIndex:0]]; 

     if (![string isEqual:@""]) { 
      NSMutableDictionary*dic = [[NSMutableDictionary alloc]init]; 

      //  NSLog(@"%@", string); 

      NSData*data = [[NSData alloc] initWithData:[string dataUsingEncoding:NSUTF8StringEncoding]]; 
      NSMutableDictionary* pars; 
      @try { 
       pars = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; 
      } 
      @catch (NSException *exception) { 
       NSLog(@"%@ , %@", exception.description, exception.reason); 
      } 
      @finally { 
      } 
      //  NSMutableDictionary* pars =[[NSMutableDictionary alloc]initWithDictionary:[NSJSONSerialization JSONObjectWithData:data 
      //             options:kNilOptions/*NSJSONReadingMutableContainers*/ error:nil]]; 

      [pars retain]; 

      [dic setObject:[[pars objectForKey:@"nm"]mutableCopy] forKey:@"nm"]; 
      [dic setObject:[[pars objectForKey:@"btr"]mutableCopy] forKey:@"btr"]; 
      [dic setObject:[[pars objectForKey:@"id"]mutableCopy] forKey:@"id"]; 
      [dic setObject:[[pars objectForKey:@"cntr"]mutableCopy] forKey:@"cntr"]; 
      [dic setObject:[[pars objectForKey:@"gnr"]mutableCopy] forKey:@"gnr"]; 

      [pars release]; 

      @try { 
       [parsedData addObject:[NSDictionary dictionaryWithDictionary:dic]]; 
      } 
      @catch (NSException* exc) { 
       NSLog(@"%@, %@", exc.description, exc.reason); 
      } 
      [dic release]; 
      [data release]; 
      [string release]; 
      [stations removeObjectAtIndex:0]; 

//   if (i%1000==0) { 
//    NSLog(@"nnnn %i %i", parsedData.count, stations.count); 
//   } 

      i++; 
      float k = count; 
      k = (i + 1)/k; 

      [self performSelectorOnMainThread:@selector(increaseProgress:) withObject:[NSNumber numberWithFloat:k] waitUntilDone:YES]; 
     } 
     else { 
      [stations removeObjectAtIndex:0]; 
     } 
    } 

    [stations release]; 
    i = 0; 

    while (parsedData.count > 0) { 
     Station*station = [[Station alloc] initWithEntity:entity insertIntoManagedObjectContext:nil]; 

     [station setName:[[[parsedData objectAtIndex:0] objectForKey:@"nm"]mutableCopy]]; 

     [station setBit:[[[parsedData objectAtIndex:0] objectForKey:@"btr"]mutableCopy]]; 
     [station setEnabled:[NSNumber numberWithInt:1]]; 

     //encoding id of the station 
     unsigned int tempInt; 
     NSScanner *scanner= [[NSScanner alloc] init]; 
     scanner = [NSScanner scannerWithString:[[parsedData objectAtIndex:0] objectForKey:@"id"]]; 

     [scanner scanHexInt:&tempInt]; 

     NSNumber *numb = [[NSNumber alloc] init]; 
     numb = [NSNumber numberWithInt:tempInt]; 
     numb = [NSNumber numberWithInt: ([numb integerValue]^sec)]; 

     [station setNumber: [NSNumber numberWithInt:[numb intValue]]]; 

     //encoding country ID 
     tempInt = 0; 
     scanner = [NSScanner scannerWithString:[[parsedData objectAtIndex:0] objectForKey:@"cntr"]]; 

     [scanner scanHexInt:&tempInt]; 
     numb = [NSNumber numberWithInt:tempInt]; 
     numb = [NSNumber numberWithInt:(([numb integerValue]^sec))]; 

     if (![numb isEqualToNumber:[NSNumber numberWithInt:n]]) 
     { 
      [station setCountryID:[NSNumber numberWithInt:[numb intValue]]]; 
     } 
     else 
     { 
      [station setCountryID:[NSNumber numberWithInt:-1]]; 
     } 

     //encoding genre ID 
     tempInt = 0; 
     scanner = [NSScanner scannerWithString:[[parsedData objectAtIndex:0] objectForKey:@"gnr"]]; 

     [scanner scanHexInt:&tempInt]; 
     numb = [NSNumber numberWithInt:tempInt]; 
     numb = [NSNumber numberWithInt:(([numb integerValue]^sec))]; 

     if (![numb isEqualToNumber:[NSNumber numberWithInt:N]]) 
     { 
      [station setGenerID:[NSNumber numberWithInt:[numb intValue]]]; 
     } 
     else 
     { 
      [station setGenerID: [NSNumber numberWithInt:-1]]; 
     } 

     [station setOrder:[NSNumber numberWithInt:i]]; 

     [context insertObject:station]; 

     float k = count; 
     k = (i + 1)/k; 

     [self performSelectorOnMainThread:@selector(increaseProgress:) withObject:[NSNumber numberWithFloat:k] waitUntilDone:YES]; 

     //  NSLog(@"%i", i); 
     [parsedData removeObjectAtIndex:0]; 
     [station release]; 
//  [station release]; 

     i++; 
    } 

    [parsedData release]; 

    [self performSelectorOnMainThread:@selector(deleteAllFromDB) withObject:nil waitUntilDone:YES]; 
    [context save:nil]; 
} 

我不明白爲什麼我的應用程序使用,以便mutch內存(〜150 MB)。我試着用分析來看我的代碼。沒有什麼興趣xCode認爲一切都好。 以下是我與儀器

Instruments

回答

0

你alloced在循環和德在掃描儀alloced循環外還有其他錯誤,這樣看。看一看,並做一個代碼審查。

0
 id obj1 =[[pars objectForKey:@"nm"]mutableCopy]; 
     [dic setObject:obj1 forKey:@"nm"]; 
     [obj1 release]; // No memory leak 
     [dic setObject:[[pars objectForKey:@"btr"]mutableCopy] forKey:@"btr"];//memory leak 
     [dic setObject:[[pars objectForKey:@"id"]mutableCopy] forKey:@"id"];//memory leak 
     [dic setObject:[[pars objectForKey:@"cntr"]mutableCopy] forKey:@"cntr"];//memory leak 
     [dic setObject:[[pars objectForKey:@"gnr"]mutableCopy] forKey:@"gnr"];//memory leak 

改變所有setObject:forKey: