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];
}

私のアプリがなぜそんなにムッチなメモリ(〜150mb)を使うのか理解できません。私はanalyzeで自分のコードを調べようとしました。興味のあるxCodeは、すべてが大丈夫だとは考えていません。これが私がInstrumentsで見たものです

楽器

4

2 に答える 2

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:

于 2013-02-13T10:11:34.413 に答える
0

ループ内で割り当てを行い、スキャナーでループ外で割り当てを解除した場合、このような他の間違いがあります。見て、コードレビューをしてください。

于 2013-02-13T10:10:14.057 に答える