0

以下のコード スニペットは、ビルドと分析を試みているときにリークします。

このコードの問題は何ですか、教えてください

- ( 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];
}
4

1 に答える 1

2

インスタンスを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];
}
于 2012-05-11T06:07:32.017 に答える