内存泄漏'Select Statement'iPhone

时间:2011-03-04 20:35:41

标签: iphone sql memory-leaks

嗨,经过几个小时的搜索,我找到了让我内存泄漏的功能。我得到的泄漏是:

Leaked Object#AddressSize   Responsible Library Responsible Frame
NSCFString  2   < multiple >    32  Foundation  
-[NSPlaceholderString initWithBytes:length:encoding:]

我看不出这段代码有什么问题。感谢Dan的任何帮助

  -(void)readItems {


    if (!database) return; // earlier problems
    // build select statement
    if (!selStmt)
    {
        const char *sql = "SELECT * FROM Demotivate order by name asc;";
        if (sqlite3_prepare_v2(database, sql, -1, &selStmt, NULL) != SQLITE_OK)
        {

            selStmt = nil;


        }
    }
    if (!selStmt)
    {
        NSAssert1(0, @"Can't build SQL to read items [%s]", sqlite3_errmsg(database));
    }

    // loop reading items from list
    [items removeAllObjects]; // clear list for rebuild
    int ret;
    while ((ret=sqlite3_step(selStmt))==SQLITE_ROW) 
    { // get the fields from the record set and assign to item
        // primary key
        NSInteger n = sqlite3_column_int(selStmt, 0); 
        Item *item = [[Item alloc] initWithPrimaryKey:n]; // create item
        // item name
        char *s = (char *)sqlite3_column_text(selStmt, 1);
        if (s==NULL) s = "";
        item.name = [NSString stringWithUTF8String:(char *)s];
        // quantity needed
        item.howOften = sqlite3_column_int(selStmt, 3);
        // noted
        s = (char *)sqlite3_column_text(selStmt, 2);
        if (s==NULL) s = "";
        item.Cost = [NSString stringWithUTF8String:(char *)s];
        s = (char *)sqlite3_column_text(selStmt, 4);
        if (s==NULL) s = "";
        item.income = [NSString stringWithUTF8String:(char *)s];
        s = (char *)sqlite3_column_text(selStmt, 5);
        if (s==NULL) s = "";
        item.wage = [NSString stringWithUTF8String:(char *)s];

        [items addObject:item]; // add to list
        [item release]; 
        // free item
    }
    sqlite3_reset(selStmt); // reset (unbind) statement


}

1 个答案:

答案 0 :(得分:0)

可能你忘了在Item的dealloc中释放item.name。