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

但我认为问题应该是:这是从数据库中获取内容的好方法吗?坦率地说,我认为如果你将阅读内容从数据库部分移到方法中,你的代码会更好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 回答