IOS 潜在泄漏对象消息

标签 ios xcode memory-management memory-leaks

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

enter image description here

最佳答案

你正在分配 ct 而从不释放它:

ct = [[CountriesDTO alloc] init];

在那里使用自动释放:

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

关于IOS 潜在泄漏对象消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19973798/

相关文章:

ios - 无法存档 ios 应用程序

ios - ReactiveObjC.framework 为 CFBundleSupportedPlatforms 键指定一个模拟器平台

ios - SQLite 语句 INSERT OR REPLACE INTO 我不清楚

ios - UIScrollView 中的动画转换

ios - 如何将 UIActivityIndi​​catorView 添加到 UITableView 的底部,并在加载时切换它

iphone - 了解何时对对象调用保留?

objective-c - 如何在xcode中找到string.h的包含路径

ios - Swift:如何使用扩展表添加应用程序共享功能

ruby-on-rails - Ruby 2.2 在 Heroku 上有内存问题吗?

iphone - NSNumber 超出范围?