0
- (id)init {
    if (self == [super init]) {
        entries = [[NSMutableArray alloc] init];
        NSString *file = [[NSBundle mainBundle] pathForResource:@"qanda" ofType:@"db"];
        sqlite3 *database = NULL;

        if (sqlite3_open([file UTF8String], &database) == SQLITE_OK) {
            sqlite3_exec(database, "select * from qanda order by question", LoadEntriesCallback, entries, NULL);
        }

        sqlite3_close(database);
    }
    return self;
}

leak near entries object: instance variable used while 'self' is not set to the result of [ super of self[init ...]]

leak near self: returning while 'self' is not set to the result of [ super of self[init ...]]

4

4 に答える 4

1

あなたが持っている必要があります

self = [super init]; 

いいえ

self == [super init];

使用する最も一般的なパターンは次のとおりです。

self = [super init];
if (self) {
    // Initialisation code
}
return self;

あなたの例では、 [super init] の結果を何にも割り当てておらず、ハングしたままになっているため、リークとメッセージが発生しています。

タイトルの質問に従ってエントリをリリースする場合:

entries = [[[NSMutableArray alloc] init] autorelease];

また

entries = [[NSMutableArray alloc] init];

[entries release];

returnまたは の前dealloc

于 2012-07-16T14:15:15.687 に答える
0

を置き換えることでエントリを解放できます:

entries = [[NSMutableArray alloc] init];

と :

entries = [[[NSMutableArray alloc] init] autorelease];

しかし、私は質問があるべきだと思います:これはdbからコンテンツを取得する良い方法ですか?率直に言って、読み取りコンテンツをdb部分からloadDataメソッドに移動した場合、コードはより良くなると思います.

于 2012-07-16T13:40:41.340 に答える
0

作成中に自動解放する必要があります。

entries = [[[NSMutableArray alloc] init] autorelease];
于 2012-07-16T13:41:10.707 に答える
0

交換してみる

entries = [[NSMutableArray alloc] init];

self.entries = [[[NSMutableArray alloc] init]autorelease];

そして交換

sqlite3_exec(database, "select * from qanda order by question", LoadEntriesCallback, entries, NULL);

sqlite3_exec(database, "select * from qanda order by question", LoadEntriesCallback, self.entries, NULL);
于 2012-07-16T14:16:23.063 に答える