0

请帮忙,我真的在努力解决内存泄漏问题。这是我的代码

+ (void) getInitialDataToDisplay:(NSString *)dbPath {

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [appDelegate.itemArray removeAllObjects];  //0.8%
    if (sqlite3_open([dbPath UTF8String], &dataBase) == SQLITE_OK) //18.9%
    {
        NSString *sqlStr = @"select * from ItemTable order by Status_id asc, ReleaseDate desc";
        const char *sql = [sqlStr UTF8String];
        sqlite3_stmt *selectstmt;
        if(sqlite3_prepare_v2(dataBase, sql, -1, &selectstmt, NULL) == SQLITE_OK) { //25%
            while(sqlite3_step(selectstmt) == SQLITE_ROW) { //46.3
                NSInteger primaryKey = sqlite3_column_int(selectstmt, 0);
                DataBaseClass *itemObj = [[DataBaseClass alloc] initWithPrimaryKey:primaryKey];  // 2.6%

                itemObj.itemID = sqlite3_column_int(selectstmt, 0);
                itemObj.itemName = ((char *)sqlite3_column_text(selectstmt,1)) ? [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)] : nil;
                itemObj.availableIcon = ((char *)sqlite3_column_text(selectstmt,2)) ? [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 2)] : nil;
                itemObj.notAvialableIcon = ((char *)sqlite3_column_text(selectstmt,3)) ? [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,3)] : nil;
                itemObj.itemReleaseDate = ((char *)sqlite3_column_text(selectstmt, 4)) ? [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 4)] : nil;
                itemObj.itemStatus = ((char *)sqlite3_column_text(selectstmt,5)) ? [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 5)] : nil;
                itemObj.itemModDate = ((char *)sqlite3_column_text(selectstmt,6)) ? [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 6)] : nil;
                itemObj.status_id = sqlite3_column_int(selectstmt, 7);
                itemObj.availableLocalIconPath = ((char *)sqlite3_column_text(selectstmt,8)) ? [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 8)] : nil;
                itemObj.notAvialableLocalIconPath = ((char *)sqlite3_column_text(selectstmt,9)) ? [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 9)] : nil;

                [appDelegate.itemArray addObject:itemObj];
            }
        }
        sqlite3_finalize(selectstmt);
    }
    else
        sqlite3_close(dataBase); //Even though the open call failed, close the database connection to release all the memory.
}

我在这段代码的某些行中遇到了内存泄漏。不知道如何解决这个问题。我在我的应用程序中使用 ARC。我在启用僵尸模式的 XCode 中使用 Instruments 工具跟踪了这一点。而且我还提到了某些行中发生泄漏的百分比。请检查代码并提供帮助。

4

1 回答 1

0

如果成功,将调用 sqlite3_finalize,但您永远不会关闭数据库。由于您也使用此方法打开它,因此您也应该在此处关闭它。删除最后一个'else',看看会发生什么。

于 2012-08-23T16:33:38.187 回答