Iphone 物体的潜在泄漏
我面临分配对象的潜在泄漏。那么,如何在循环中释放自定义类对象呢。随信附上我的代码Iphone 物体的潜在泄漏,iphone,objective-c,ios,ios4,Iphone,Objective C,Ios,Ios4,我面临分配对象的潜在泄漏。那么,如何在循环中释放自定义类对象呢。随信附上我的代码 - (ProfileClass *) getUserProfile { } 如果我自动释放我的个人资料=[[ProfileClass alloc]getProfileInfo:userName with Email:userEmail with Phone:phoneNum]自动释放];所以我的应用程序后来崩溃了。因此,我发布了if-check,但build and Analyze将其显示为警告。为什么不这样做:
- (ProfileClass *) getUserProfile
{
}
如果我自动释放我的个人资料=[[ProfileClass alloc]getProfileInfo:userName with Email:userEmail with Phone:phoneNum]自动释放];所以我的应用程序后来崩溃了。因此,我发布了if-check,但build and Analyze将其显示为警告。为什么不这样做:
return [profile autorelease];
而且也没有必要这样做
if (profile)
检查。只要无条件地释放即可。如果profile
为零,则不会产生任何负面影响
FWIW:我不太明白你的
getProfile:etc.
方法的作用。我假设它是一个初始值设定项,仅此而已(就像Cocoa中的许多initXYZ:
方法)。如果是这样的话,您可能应该将其称为initWithUserName:email:phone:
,以符合惯例。您可以发布该方法吗?使用数组,您可以在调用此方法之前解决此问题
NSMutableArray *ProfileArray=[[NSMutableArray alloc] initWithArray:[ClassObj getUserProfile]];
ProfileClass *profileObj=[[ProfileArray objectAtIndex:0] retain];
[ProfileArray release];
// now you can use profile object anywhere... I hope memory issue is also solved
- (NSMutableArray *) getUserProfile
{
NSMutableArray *array=[[NSMutableArray alloc] init];
NSString *query = [NSString stringWithFormat:@"SELECT * FROM Profile"];
NSLog(@"query %@",query);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"MOFAdb.sqlite"];
ProfileClass *profile = nil;
// Open the database. The database was prepared outside the application.
if(sqlite3_open([path UTF8String], &database) == SQLITE_OK)
{
sqlite3_stmt *Statement1;
//int i=0;
if (sqlite3_prepare_v2(database, [query UTF8String], -1, &Statement1, NULL) == SQLITE_OK) {
//int returnValue = sqlite3_prepare_v2(database, sql, -1, &Statement1, NULL);
if (sqlite3_step(Statement1) == SQLITE_ROW) {
// The second parameter indicates the column index into the result set.
NSString *userName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(Statement1, 0)];
NSString *userEmail = [NSString stringWithUTF8String:(char *)sqlite3_column_text(Statement1, 1)];
NSString *phoneNum = [NSString stringWithUTF8String:(char *)sqlite3_column_text(Statement1, 2)];
//int phone = sqlite3_column_int(Statement1, 2);
//NSLog(@"%d",phone);
//RecipeClass *rc = [[RecipeClass alloc] getRecipe:recipeName withRecipeIng:recipeIng withRecipeInst:recipeInstru withRecipeTips:recipeTips withRecipeDesc:recipeDesc];
if (profile)
[profile release];
profile = [[ProfileClass alloc] getProfileInfo:userName withEmail:userEmail withPhone:phoneNum];
[array addObject:profile];
[profile release];
}
}
//Release the select statement memory.
sqlite3_finalize(Statement1);
//}
}
else {
// Even though the open failed, call close to properly clean up resources.
sqlite3_close(database);
NSAssert1(0, @"Failed to open database with message '%s'.", sqlite3_errmsg(database));
// Additional error handling, as appropriate...
}
return [array autorelease];
}
我希望这对你有帮助
cheers您的方法:-(ProfileClass*)getUserProfile
不是实例方法或副本。您应该返回自动删除的对象。但是你应该在最后一行做,因为你有一个if/else结构,如果你只在第行自动释放它,profile=[[ProfileClass alloc]getProfileInfo:userName with email:userEmail with phone:phoneNum]autorelease]代码>如果它在if语句中失败并转到else,它将不会自动释放。那么就这么做吧:
return [profile autorelease];
您也可以像这样自动释放:
返回[配置文件自动释放]
并将ProfileClass的对象保留在使用它的位置
Ex-ProfileClass*objProfile=[[database getUserProfile]retain]
并在使用objProfile时释放它。Er,您在任何地方都有init
呼叫吗?您需要发布getProfileInfo:withEmail:withPhone:
。因为您总是从这个ProfileClass*profile=nil开始;如果(profile)[profile release],以下代码将永远不会执行@user366584 b/c您的错误消失并不意味着您解决了它。在给出建议之前,请阅读内存管理。你用[[mission autorelease]retain]做了什么;这是错误的。保留要自动删除的对象。这背后的原因是什么。对我来说,你是瞎开枪,只需键入与内存管理有关的内容就可以解决问题。@user366584:Cyprian是对的。“它有效”并不意味着你所做的是正确的。在这种情况下,这是没有意义的。什么是mission
?很抱歉,还有另一个类名mission,所以mission与profileFWIW是同一个实例变量,它是一个实例方法,因为它在单个分配的实例上运行。它可能只是一个名称错误的初始值设定项。@Cyprian它崩溃了,所以我在上面的评论中建议了一个解决方案,但它不是正确的内存管理,但它与我的返回[[profile autorelease]retain]??不,这是不一样的,因为如果保留任何对象,则表示您是该对象的所有者。只有您可以释放它。所以您不能在其他类中释放它。
return [profile autorelease];