0

I made a class method for read a plist file, and it works fine. I changed in ARC and now the class look like:

+ (NSDictionary *)readPlistDic:(NSString*)filePlist
{
    NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *filePath = [[NSString alloc] initWithString:[[documentsDirectoryPath stringByAppendingPathComponent:filePlist]stringByAppendingPathExtension:@"plist"]];
    NSString *path = [[NSBundle mainBundle] bundlePath];
    NSString *startPath = [[path stringByAppendingPathComponent:filePlist]stringByAppendingPathExtension:@"plist"];
    if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]){
        if([[NSDictionary dictionaryWithContentsOfFile:startPath] writeToFile:filePath atomically:NO])
            NSLog(@"File dei profili Copiato nell App DocumentsDirectory\n\n");
        else
            NSLog(@"Errore durante la copia in DocumentsDirectory\n\n");
    }
    NSDictionary *dicReturn = [[NSDictionary alloc] initWithContentsOfFile:filePath];
    return dicReturn;
#warning    return [NSDictionary dictionaryWithContentsOfFile:filePath];
}

it works, but generate a leaks

enter image description here

what i'm wrong? any suggestion? thanks at all!

UPDATE i write the plist here:

NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithDictionary:[ReadWritePlistNew readPlistDic:@"PartitaDefault"]];

    NSDate *dataOggi = [NSDate date];
    [dic setValue:dataOggi forKey:Data_Partita];

    NSNumber *idPartitaNum = [NSNumber numberWithInt:ultimoID+1];
    [dic setValue:idPartitaNum forKey:ID_Partita];


    NSArray *salvaGioc = [NSArray arrayWithObjects:ID_GIOCATORE_NORD,ID_GIOCATORE_SUD,ID_GIOCATORE_EST,ID_GIOCATORE_OVEST, nil];
    for (int i=0; i<4; i++) {
        [dic setValue:[NSNumber numberWithInt:[[[self.players objectAtIndex:i] objectForKey:defID_Player] intValue]] forKey:[salvaGioc objectAtIndex:i]];
    }

    NSNumber *numGiocatori = [NSNumber numberWithInt:numeroGiocatori];
    [dic setValue:numGiocatori forKey:GIOCATORI_NUM];
    [dic setValue:[NSNumber numberWithInt:tavoloNumero]    forKey:TAVOLO_NUMERO];


    [dic setValue:[NSNumber numberWithBool:isEXP] forKey:EXP_MODE];

    [dic setValue:[NSNumber numberWithBool:isVPAttivoLocal] forKey:VictoryPointAttivo];

    if (isVPAttivoLocal) {
        [dic setValue:[NSNumber numberWithInt:numeroSmazzateIndex] forKey:NumeroSmazzateIndex];
    }
//GPS#warning salvare gps
    //NSLog(@"isCustomVP_Local  %@",isCustomVP_Local ? @"Attivo":@"Non attivo");
    [dic setValue:[NSNumber numberWithBool:isCustomVP_Local] forKey:ISVP_CUSTOM];
    [dic setValue:[NSString stringWithFormat:@"%d",puntiMax] forKey:PUNTI_FINE_PARTITA];


    //salva tutto sul plist database
    NSMutableDictionary *dicOld = [[NSMutableDictionary alloc] initWithDictionary:[ReadWritePlistNew readPlistDic:@"PartiteDB"]];
    NSMutableArray *arrToSave = [[NSMutableArray alloc] initWithArray:[dicOld objectForKey:@"Partite"]];
    //NSLog(@"dic: %@",dic);
    [arrToSave addObject:dic];
    [dicOld setValue:arrToSave forKey:@"Partite"];
    [ReadWritePlistNew writeToPlist:@"PartiteDB" dic:dicOld];

and [ReadWritePlistNew writeToPlist:]:

+ (void)writeToPlist:(NSString*)filePlist arr:(NSArray*)arr
{
    NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *filePath = [[documentsDirectoryPath stringByAppendingPathComponent:filePlist]stringByAppendingPathExtension:@"plist"];
    [arr writeToFile:filePath atomically:YES];

}
Simone Pistecchia
  • 2,746
  • 3
  • 18
  • 30
  • It possible that one of the objects in the dictionary leaks (not the dictionary itself). Does static analysis show you anything? – Firoze Lafeer Mar 23 '13 at 13:49
  • hi, static analysis it's succeeded... – Simone Pistecchia Mar 23 '13 at 14:02
  • @Firoze Lafeer what do you think about this line? NSDate *dataOggi = [NSDate date]; [dic setValue:dataOggi forKey:Data_Partita]; dataOggi is better to archive like string? – Simone Pistecchia Mar 23 '13 at 14:18
  • I'm not sure what you mean by archive like string? So are you saying the static analyzer showed no results? With ARC, leaks are often caused by reference cycles. Maybe post more code where you use the objects in this dictionary. – Firoze Lafeer Mar 23 '13 at 15:47
  • The leaks tool shows you what type of object was leaked. Can you show a screenshot of that? – Firoze Lafeer Mar 23 '13 at 18:23

0 Answers0