0

我正在开发一个 IOS 应用程序。我完全理解内存管理的逻辑。我确实使用 Xcode 工具进行了分析,然后我看到了“潜在的对象泄漏”消息。下面的代码有什么问题。你能帮我吗 ?

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

在此处输入图像描述

4

1 回答 1

4

您正在分配 ct 并且从不释放它:

ct = [[CountriesDTO alloc] init];

在那里使用自动释放:

ct = [[[CountriesDTO alloc] init] autorelease];
于 2013-11-14T09:24:33.050 回答