我尝试做一个应用程序,但是内存分配崩溃了很多,然后我尝试减少所有代码并清理它,现在我得到了这个e
为什么我得到:
在第101行分配的对象的潜在泄漏并存储在'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;
}
答案 0 :(得分:2)
如果您不使用ARC(自动引用计数),则应将livello标记为自动释放。尝试将此作为您的退货声明:
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/