hiwordls
hiwordls

Reputation: 781

IOS Potential Leak an Object Message

I am developing an IOS application. I am fully understand the logic of the memory management. I did analyze with Xcode instruments then I saw "Potential leak an object" message. What is wrong with the code below. Can you help me please ?

-(CountriesDTO*)getNationalityCountry{

    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    NSString *userNationality = (([def stringForKey:@"passCitizenshipCountry"]!=nil && [def stringForKey:@"passCitizenshipCountry"]!=NULL)?[def stringForKey:@"passCitizenshipCountry"]:@"");

    CountriesDTO *ct = [self getCountryForCode:userNationality];

    return ct; **//Potential leak of an object**
}

-(CountriesDTO*)getUserPhoneCodeCountry{

    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    NSString *userPhoneCode = (([def stringForKey:@"countryPhoneFlagCode"]!=nil && [def stringForKey:@"countryPhoneFlagCode"]!=NULL)?[def stringForKey:@"countryPhoneFlagCode"]:@"");

    CountriesDTO *ct = [self getCountryForCode:userPhoneCode];

    return ct; **//Potential leak of an object**

}
-(CountriesDTO*)getCountryForCode:(NSString*)candidateCode{

    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    NSString *userSiteCountry = (([def stringForKey:@"userSiteUrlCountry"]!=nil && [def stringForKey:@"userSiteUrlCountry"]!=NULL)?[def stringForKey:@"userSiteUrlCountry"]:@"");

    NSString *defCode = @"";

    if (![candidateCode isEqualToString:@""]) {
        //Kullanıcının profilindeki nationality veya phone code dolu ise bu kullanılır.
        defCode = candidateCode;

    }else{
        defCode=@"TR";
    }

    DatabaseController *db = [[[DatabaseController alloc] init] autorelease];
    CountriesDTO *ct = [db getCountry:defCode];

    if (ct==nil) {
        ct = [[CountriesDTO alloc] init];
        ct.countryCode_=@"";
        ct.countryId_=@"";
        ct.countryName_=@"";
        ct.countryPhoneCode_=@"";
    }

    return ct; **//Potential leak of an object**
}

-(CountriesDTO*)getCountry:(NSString*)countryCode{

    CountriesDTO *model=[[[CountriesDTO alloc] init] autorelease];

    if (![db open]) {
        [db release];

    }else{

        FMResultSet *s = [db executeQuery:[NSString stringWithFormat:@"SELECT * FROM country Where code ='%@'",countryCode]];

        while ([s next]) {
            //retrieve values for each record

            [model setCountryId_:[s stringForColumn:@"id"]];
            [model setCountryCode_:[s stringForColumn:@"code"]];
            [model setCountryPhoneCode_:[s stringForColumn:@"phoneCode"]];
            [model setCountryName_:[s stringForColumn:@"name"]];
        }

        [db close];
    }

    return model;
}

enter image description here

Upvotes: 0

Views: 337

Answers (1)

Antonio MG
Antonio MG

Reputation: 20410

You are allocating ct and never releasing it:

ct = [[CountriesDTO alloc] init];

Use autorelease there:

ct = [[[CountriesDTO alloc] init] autorelease];

Upvotes: 4

Related Questions