iPhone应用程序中的内存问题

时间:2012-08-23 12:27:47

标签: iphone objective-c memory-leaks dealloc

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

+ (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。我在Zombie启用模式下使用XCode中的工具工具跟踪了这一点。而且我还提到了某些线路发生泄漏的百分比。请检查代码并提供帮助。

1 个答案:

答案 0 :(得分:0)

如果您成功,则会调用sqlite3_finalize,但您永远不会关闭数据库。既然你也在这个方法中打开它,你也应该在这里关闭它。删除最后的“其他”,看看会发生什么。