0

我尝试做一个应用程序,但内存分配出现很多崩溃,然后我尝试减少所有代码并清理它,现在我得到了这个

为什么我得到:

在第 101 行分配并存储到“liveello”中的对象的潜在泄漏

- (id) leggiLivelloDaTabella:(NSString *)tabella {

NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *databasePath = [documentsDir stringByAppendingPathComponent:@"DataBase.sqlite"];

sqlite3 *database;

Livello *livello = nil;

if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
    // query che ricava i valori
    const char *sql =  [[NSString stringWithFormat:@"select * from Livelli WHERE Tabella = '%@'",tabella] UTF8String];

    sqlite3_stmt *selectstmt;

    if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
        while(sqlite3_step(selectstmt) == SQLITE_ROW) {
            // ricaviamo i valori letti dalla query
            NSString *nomeTabella = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 0)];
            NSString *risposteGiuste = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];
            NSString *stato = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 2)];
            NSString *risposteMinime = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 3)];
            NSString *tentativiSbagliati = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 4)];
            NSString *tentativiTotali = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 5)];    
            NSString *popUp = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 6)];
            NSString *idLivello = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 7)];

            livello = [[Livello alloc] initLivelloWithTabella:nomeTabella
                                                            rispGiuste:risposteGiuste
                                                          statoLivello:stato 
                                                            rispMinime:risposteMinime 
                                                    tentativiSbagliati:tentativiSbagliati
                                                          tentativiTot:tentativiTotali
                                                          disaplyPopUp:popUp
                                                                idLevel:idLivello];
        }
    }
    sqlite3_finalize(selectstmt);
    sqlite3_close(database);
    selectstmt = nil;
}
else
    sqlite3_close(database);

return  livello;

}

4

1 回答 1

2

如果你没有使用 ARC(自动引用计数),你应该将 livello 标记为 autorelease。尝试将其用作您的退货声明:

return [livello autorelease];

以下是有关 ARC 的更多信息:

http://maniacdev.com/ios-5-sdk-tutorial-and-guide/arc-automatic-reference-counting/

http://longweekendmobile.com/2011/09/07/objc-automatic-reference-counting-in-xcode-explained/

于 2012-06-18T18:03:04.777 回答