【问题标题】:IOS Potential Leak an Object MessageIOS 潜在泄漏对象消息
【发布时间】:2013-11-14 09:22:40
【问题描述】:

我正在开发一个 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;
}

【问题讨论】:

  • 请注意,对于每个分析器警告,您都可以单击蓝色小图标以获取有关问题的详细报告。
  • @MartinR 添加了屏幕截图。谢谢
  • 我确信下面的答案是正确的 - 但请尝试理解分析器消息,这将在未来有所帮助。

标签: ios xcode memory-management memory-leaks


【解决方案1】:

你正在分配 ct 并且永远不会释放它:

ct = [[CountriesDTO alloc] init];

在那里使用自动释放:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-25
    • 2012-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多