【问题标题】:Memory Leak 'Select Statement' iPhone内存泄漏“选择语句”iPhone
【发布时间】:2011-03-04 20:35:40
【问题描述】:

嗨,经过几个小时的搜索,我找到了导致内存泄漏的函数。我得到的泄漏是:

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

我看不出这段代码有什么问题。任何帮助将不胜感激丹

  -(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


}

【问题讨论】:

  • 我们需要Item类的定义

标签: iphone sql memory-leaks


【解决方案1】:

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

【讨论】:

  • 谢谢你是对的,但是在它之后发布,当第二次调用它时,它会使我的应用程序崩溃。我应该在哪里释放它在while循环中声明的位置,所以不能在delloc中释放。
  • 感谢您的帮助。我的逻辑有点混乱,我没有在我的项目类中发布变量。
猜你喜欢
  • 2010-11-23
  • 2011-12-10
  • 2011-05-24
  • 2023-03-20
  • 1970-01-01
相关资源
最近更新 更多