Question

I try to do an app but I get a lot of crash an memory allocation, then I try to reduce all the code and clean it, now I get this e

Why I get:

Potential leak of an object allocated on line 101 and stored into 'livello'

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

}

Was it helpful?

Solution

If you are not using ARC (automatic reference counting), you should be marking livello as autorelease. Try using this as your return statement:

return [livello autorelease];

Here is some more info on 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/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top