内存泄漏:如何停止? [英] Memory leak : How to stop?

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

问题描述


我使用NSObject类来获取所有的数据从数据库。当我调用该函数时,它将显示一个内存泄漏,我使用stringWithFormat分配一个字符串。我想,当我们使用这种方法,我们不应该释放那个字符串,为什么它显示内存泄漏?

I am using NSObject class to get all the data from a database. When I call that function it will show a memory leak where I assign a string using stringWithFormat. I think when we use this method we should not have to release that string so why does it show a memory leak?

提前感谢。 :)

- (void) Allnote
{
   [app.NoteArray removeAllObjects];
   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   NSString *documentsDirectory = [paths objectAtIndex:0];
   NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Notes.sqlite"];
   if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) 
   {
      NSString *querySQL = [NSString stringWithFormat:@"SELECT * From note" ];
      const char *sql = [querySQL UTF8String];
      sqlite3_stmt *searchStatement;

    if (sqlite3_prepare_v2(database, sql, -1, &searchStatement, NULL) == SQLITE_OK) 
    {
        while (sqlite3_step(searchStatement) == SQLITE_ROW) 
        {
            noteClass *noteObj = [[noteClass alloc]init];

            noteObj.noteTitle = [NSString stringWithUTF8String:(char *)sqlite3_column_text(searchStatement, 0)];   // at this place
            noteObj.noteContent = [NSString stringWithUTF8String:(char *)sqlite3_column_text(searchStatement, 1)]; // at this place

            NSNumber *noteId = [NSNumber numberWithInt:sqlite3_column_int(searchStatement, 2)];

            noteObj.noteId = [NSString stringWithFormat:(@"%@"),noteId]; // at this place

            files=noteObj.noteTitle;
            filescont=noteObj.noteContent;
            [app.NoteArray addObject:noteObj];
            [noteObj release];

        }
    }
    sqlite3_finalize(searchStatement);

}

sqlite3_close(database);
}


推荐答案

c> stringWithFormat 是一个工厂方法意味着它的定义中有autorelease,这就是为什么ur得到内存问题。但是,这不是一个问题。100%完美。

See, stringWithFormatis a factory method means that it has autorelease in its definition, that's why u r getting memory issue. But, that is not an issue.its 100% perfect.

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

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