内存不释放

时间:2013-02-13 10:02:05

标签: objective-c memory-leaks instruments

这是我的代码:

-(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看到的内容

Instruments

2 个答案:

答案 0 :(得分:0)

你已经在循环中分配了并且在扫描程序的循环外部分配了还有其他错误。看看并进行代码审查。

答案 1 :(得分: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: