iphone - 我的代码片段泄露了

标签 iphone objective-c ios ios4

当我尝试构建和分析时,下面的代码片段泄漏了。

这段代码有什么问题,请告诉我

- ( NSString *) getSubCategoryTitle:(NSString*)dbPath:(NSString*)ID{
        NSString *subCategoryTitle;

    if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) 
    {
        NSString *selectSQL = [NSString stringWithFormat: @"select sub_category_name from sub_categories where id = %@",ID];

        NSLog(@"%@ I am creashes here", selectSQL);

        const char *sql_query_stmt = [selectSQL UTF8String];

        sqlite3_stmt *selectstmt;

        if(sqlite3_prepare_v2(database, sql_query_stmt, -1, &selectstmt, NULL) == SQLITE_OK) 
        {

            while(sqlite3_step(selectstmt) == SQLITE_ROW) 
            {

                subCategoryTitle = [[NSString alloc] initWithUTF8String:
                                    (const char *) sqlite3_column_text(selectstmt, 0)];


            }
        }

        sqlite3_finalize(selectstmt);    

    }   
    sqlite3_close(database); 


    return [subCategoryTitle autorelease];
}

最佳答案

您在循环中将实例分配到 subCategoryTitle 中,但不要释放之前的分配。

subCategoryTitle = [[NSString alloc] initWithUTF8String:
                                (const char *) sqlite3_column_text(selectstmt, 0)];

要么(自动)释放它,要么直接转到最后一行,并避免这种情况,因为它没有多大意义。

仅创建最后一个对象的示例:

char * col_text = NULL;
while(sqlite3_step(selectstmt) == SQLITE_ROW) 
{
    col_text = sqlite3_column_text(selectstmt, 0);
}
if (col_text != NULL)
{
    subCategoryTitle = [[NSString alloc] initWithUTF8String:col_text];
}

关于iphone - 我的代码片段泄露了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10546222/

相关文章:

ios - 通过动画和中心更改宽度 UIView

c# - iPhone - 未经授权的访问异常

iphone - 我如何将 NSData 转换为 NSArray?

objective-c - setShowsTouchWhenHighLighted 不起作用

ios - 自定义 MKAnnotation 标注气泡

ios - OpenGL 中的平滑颜色混合

iphone - 每次留下透明区域时,UIView ContentMode ScaleAspectFit 是否缩放图像

iphone - 删除 iTunes 连接中的 iPhone 应用程序版本?

ios - 在 objective-c 中解析 xml 时更改 View

ios - 当用户位于 UITableView 底部时重新加载 UITableView 时出现 Jerk