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

我不明白为什么我的应用程序使用如此多的内存(~150 mb)。我试图用分析来查看我的代码。没有什么有趣的 xCode 认为一切正常。这是我在 Instruments 中看到的

仪器

4

2 回答 2

0

您已在循环中分配并在扫描仪的循环外取消分配,还有其他类似的错误。看看并进行代码审查。

于 2013-02-13T10:10:14.057 回答
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 回答