iOS FMDB内存泄漏 [英] ios fmdb memory leaks

查看:182
本文介绍了iOS FMDB内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

hi Im asking this question for my friend, who is struggling with this fmdb memory issue. his project read a list from a db, and he opens the db and read the data for each cell, (which is not a good idea but he did), in init, init the database queue,

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

在cellForRowAtIndexPath中,使用configCell进行操作

in cellForRowAtIndexPath, use configCell to do things

- (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"];

}

如您所见,

从数据库读取并设置为单元格的标签; 使用仪器时,滚动表格视图时,内存分配会急剧增加(很快就会从800k增加到1.6m), 但是,如果您注释行aCell.textLabel.text = [aUserRecord objectForKey:@"user_name"]; 似乎没有比以前的情形大得多的内存泄漏(从800k到大约900k).

as you can see, read from DB and set to cell's label; with instrument, when scrolling the tableview, memory allocations increases dramatically(from 800k increases to 1.6m very soon), but if you comment the line aCell.textLabel.text = [aUserRecord objectForKey:@"user_name"]; seems no much memory leaks than the previous scenario,(from 800k to about 900k).

通过注释这一行,我们仍然在进行db工作,这很奇怪,必须将单元格与db或block链接起来,这使整个事情变得很糟糕.

by comment the line, we still do the db work, it's strange here, must be some thing links the cell and db or block, which makes the whole thing bad.

我检查了rs,aUserRecord的保留计数,它们为1,看起来很正常. 区块,数据库和FMDB的任何专家都可以分享您的意见,欢呼

I check the retain count of rs,aUserRecord they are 1, and seems normal. any expert in block, DB, and FMDB pls share your opinions, cheers

fmdb inDatabase函数将块管理到调度队列中进行处理,我试图删除该块,仅使用db来完成工作,仍然存在内存问题,请帮忙

the fmdb inDatabase function manages the block in to a dispatch queue to process, I tried to remove the block , simply use the db to do the job, memory issues remains, pls help

推荐答案

也许可以帮上忙.只是猜测.

Maybe this could help. Just guessing.

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

这篇关于iOS FMDB内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆