2

メモリリークエラーが表示されます データベースからすべてのデータを取得するために 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);
}
4

3 に答える 3

2

ファクトリメソッドとstringWithFormatは、定義に自動解放があることを意味します。そのため、メモリの問題が発生します。しかし、それは問題ではありません。100%完璧です。

于 2011-11-17T12:32:55.790 に答える
2

フォーマットを適用していない場合、なぜ使用するのかstringWithFormat

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

以下と同じです:

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

そして、あなたは正しいです、あなたはそれらを解放する必要はありません。

于 2011-11-17T12:20:42.757 に答える
1

Surya Kantの回答に加えて回答しています。

それは問題ではありませんでした。それでも、問題を回避したい場合は、1 つのことを行ってください。

次の行の代わりに、

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

他の人に対しても同様に行います。

于 2011-11-18T06:16:19.733 に答える