iphone - 物体的潜在泄漏

标签 iphone objective-c ios ios4

我面临分配对象的潜在泄漏。那么我如何在循环中释放我的自定义类对象。我在下面附上我的代码。

- (ProfileClass *) getUserProfile

{

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];

            //NSLog(@"%@",fact);
            //NSLog(@"%d",i);
            //i++;      

        }
    }

    //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 profile; 

}

如果我自动释放我的个人资料= [[[ProfileClass alloc] getProfileInfo:userName withEmail:userEmail withPhone:phoneNum] autorelease];所以我的应用程序稍后崩溃了。因此,我发布了 if 检查,但构建和分析将其显示为警告。

最佳答案

你也可以像这样自动释放:

返回[配置文件自动释放];

并保留您使用它的 ProfileClass 对象,

Ex- ProfileClass *objProfile=[[数据库 getUserProfile] 保留];

并在使用时释放 objProfile。

关于iphone - 物体的潜在泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7171865/

相关文章:

iphone - 删除 NSString 的一部分并将其复制到另一个 NSString

ios - 使用 autoLayout 在 UITableviewcell 上移动 UILabel 和 UIImageVIew

iphone - 通过 iPhone 应用程序将网络上的文件移动到不同位置

iphone - 将我的 JSON 放入 tableView

iphone - 解析 NSString 加载时间很长

iphone - 适用于 Mac/iPhone 的 xUnit 测试框架

iphone - 将 UITableViewController 与小型表格一起使用?

objective-c - 本地化串联或动态字符串

ios - 如何向 UIView 添加径向渐变?

ios - 在 iOS 中写入随机值 AudioBuffer 时无法听到 'noise'