内存泄漏:如何停止?

发布于 2024-12-16 15:48:07 字数 1692 浏览 1 评论 0原文

您可以看到内存泄漏错误 我正在使用 NSObject 类从数据库获取所有数据。当我调用该函数时,它会显示内存泄漏,我使用 stringWithFormat 分配字符串。我认为当我们使用这种方法时,我们不应该释放该字符串,那么为什么它会显示内存泄漏呢?

提前致谢。 :)

- (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);
}

You can see that memory leak error
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?

Thanks in advance. :)

- (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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

分开我的手 2024-12-23 15:48:07

看,stringWithFormat是一个工厂方法,意味着它的定义中有自动释放,这就是你遇到内存问题的原因。但是,这不是问题。它 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.

一片旧的回忆 2024-12-23 15:48:07

如果您不应用格式,那么为什么使用 stringWithFormat

NSString *querySQL = [NSString stringWithFormat:@"SELECT * From note" ];

与:

NSString *querySQL = @"SELECT * From note";

您是对的,您没有释放它们。

If you aren't apply a format, then why use stringWithFormat

NSString *querySQL = [NSString stringWithFormat:@"SELECT * From note" ];

Is the same as:

NSString *querySQL = @"SELECT * From note";

And you are correct, you do not have te release them.

酒浓于脸红 2024-12-23 15:48:07

除了苏里亚·康德的回答之外,我还做出了回应。

这不是问题。但如果你想避免这个问题,请做一件事。

不要执行以下行,而是

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

执行以下

NSString *noteTit = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(searchStatement, 0)];
noteObj.noteTitle = noteTit;
[noteTit release];

操作,对其他人也执行类似的操作。

I am responding in addition to Surya Kant's answer.

It was not a problem with that. But still if you want to avert the issue, please do onething.

Instead of the following line,

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

do as follows

NSString *noteTit = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(searchStatement, 0)];
noteObj.noteTitle = noteTit;
[noteTit release];

Similarly do it for others.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文