【问题标题】:ios fmdb memory leaksios fmdb 内存泄漏
【发布时间】:2013-09-04 08:35:20
【问题描述】:

嗨,我在为我的朋友问这个问题,他正在为这个 fmdb 内存问题而苦苦挣扎。 他的项目从数据库中读取了一个列表,然后他打开了数据库并读取了每个单元格的数据(这不是一个好主意,但他做到了), 在init中,初始化数据库队列,

 databaseQueue = [[FMDatabaseQueue alloc] initWithPath:_dbPath];

在cellForRowAtIndexPath中,使用configCell做事

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString * addressBookTableViewCellIdentifier = @"AddressBookTableViewCell";
AddressBookTableViewCell * cell = (AddressBookTableViewCell*)[tableView dequeueReusableCellWithIdentifier:addressBookTableViewCellIdentifier];
if (cell == nil)
{
    // Create a cell to display an ingredient.
    cell = [[[AddressBookTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                            reuseIdentifier:addressBookTableViewCellIdentifier]
            autorelease];
    cell.selectionStyle = UITableViewCellSelectionStyleBlue;
}
[self configCell:cell atIndexPath:indexPath];
return cell;

}

-(void)configCell:(AddressBookTableViewCell *)aCell atIndexPath:(NSIndexPath *)indexPath{

__block  NSDictionary *aUserRecord = nil;

NSString *_userName = nil;

   [databaseQueue inDatabase:^(FMDatabase *db) {

        [db open];
        int offset = 0;

        offset +=indexPath.row;

        NSString *str = [NSString stringWithFormat:@"SELECT table_ems_user.pid,\
                         table_ems_user.user_id,\
                         table_ems_user.user_name,\
                         table_ems_user.sex,\
                         table_ems_user.jid,\
                         table_ems_user.signature,\
                         table_ems_user.head\
                         FROM table_ems_user order by name_spell limit %d,1",offset];
        FMResultSet *_rs = [db executeQuery:str];
        while ([_rs next]) {
            aUserRecord = [_rs resultDictionary];
        }
        [_rs close];
        [db close];
    }];

    aCell.textLabel.text = [aUserRecord objectForKey:@"user_name"];

}

如您所见,从 DB 中读取并设置为单元格的标签; 使用仪器,滚动表格视图时,内存分配急剧增加(很快从 800k 增加到 1.6m), 但是如果你评论这行 aCell.textLabel.text = [aUserRecord objectForKey:@"user_name"]; 似乎没有比前一种情况更多的内存泄漏,(从 800k 到大约 900k)。

通过注释行,我们仍然在做 db 工作,这里很奇怪,一定是某些东西链接了 cell 和 db 或 block,这使得整个事情变得糟糕。

我检查了 rs,aUserRecord 的保留计数,它们是 1,看起来很正常。 任何块、DB 和 FMDB 方面的专家请分享您的意见,干杯

编辑:fmdb inDatabase 函数将块管理到调度队列中进行处理,我试图删除块,只需使用数据库来完成工作,内存问题仍然存在,请帮助

【问题讨论】:

    标签: ios memory-leaks block fmdb


    【解决方案1】:

    也许这会有所帮助。只是猜测。

    aCell.textLabel.text = [NSString stringWithFormat:@"%@", [aUserRecord objectForKey:@"user_name"]];
    

    【讨论】:

      猜你喜欢
      • 2013-05-03
      • 2012-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-09
      • 2016-10-16
      • 2014-06-06
      相关资源
      最近更新 更多