iphone - 涉及NSString的内存泄漏

标签 iphone objective-c cocoa-touch sqlite

我在我的应用程序中找不到导致内存泄漏的原因。我发现通过仪器存在内存泄漏,并且我调用该函数的次数超过发生内存泄漏的次数。因此很明显发生了内存泄漏。

这是我的代码。
目的:

@interface WordObject : NSObject
{
    int word_id;
    NSString *word;
}

@property int word_id;
@property (copy) NSString *word;

@end


用于填充UITableView的方法:

-(void) reloadTableData {
        [tableDataArray removeAllObjects];

        for (int i = 0; i <= 25; i++)
        {
            NSMutableArray *words_in_section = [ [NSMutableArray alloc] init];
            [tableDataArray addObject:words_in_section];
            [words_in_section release];
        }

        WordObject *tempWordObj;

        int cur_section;

        while ( tempWordObj = [ [WordsDatabase sharedWordsDatabase] getNext] )
        {   
            cur_section = toupper([ [tempWordObj word] characterAtIndex:0 ] ) - 'A';

            NSMutableArray *temp_array = [tableDataArray objectAtIndex:cur_section];
            [temp_array addObject:tempWordObj];
            [tableDataArray replaceObjectAtIndex:cur_section withObject:temp_array];
        }

        [mainTableView reloadData];

        [mainTableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];
    }


获取单元格的内容:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:cellIdentifier] autorelease];
    }


    WordObject *tempWordObj = [ [tableDataArray objectAtIndex:[indexPath section] ] objectAtIndex:[indexPath row] ];

    if (!tempWordObj)
    {
        return cell;
    }

    cell.text = [tempWordObj word];

    return cell;
}


这是我清理内存的方法:

-(void) freeMemory
{   
    if (tableDataArray)
    {
        [tableDataArray release];
        tableDataArray = nil;
    }
}


从reloadTableData调用的函数:

    -(WordObject*) getNext {
    if(sqlite3_step(getStmt) == SQLITE_DONE)
    {
        sqlite3_reset(getStmt);
        return nil;
    }


    WordObject *tempWord = [ [WordObject alloc] init];
    tempWord.word_id = sqlite3_column_int(getWordsStmt, 0);

    tempWord.word = [NSString stringWithUTF8String:(char *)sqlite3_column_text(getWordsStmt, 1)]; //Here a leak occurs

    return [tempWord autorelease];
}


泄漏的对象是[WordObject word]。

我将非常感谢任何能帮助我解决此问题的人。

最佳答案

将此方法添加到WordObject:

- (void)dealloc {
    [word release];
    [super dealloc];
}


此代码确保删除WordObject实例时释放属性word



我很确定该代码也属于dealloc方法。顺便说一句,您不需要tableDataArray = nil

- (void)freeMemory
{   
    if (tableDataArray)
    {
        [tableDataArray release];
        tableDataArray = nil;
    }
}

关于iphone - 涉及NSString的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/732072/

相关文章:

swift - CMPedometer 在 Swift 2.0 中出现错误

iphone - 如何删除自定义导航栏图像?

iphone用户名密码MD5

iphone - [NSCFString 字符串值] : unrecognized selector sent to instance

objective-c - 如何在你的 cocoa 应用程序中像聚光灯一样制作搜索字段和搜索结果?

objective-c - 离线生成二维码

cocoa-touch - 使用 RestKit 进行轻量级迁移

iphone - 使用 segue 传回数据时卡住

ios - 核心情节线程错误 - EXC_BAD_ACCESS(代码=1,地址=0x400002122)

ios - 如何在从主函数返回值之前等待委托(delegate)函数完成