Objetivo C – sqlite3_open Fuga de memory aunque sqlite3_close () aplicado

Recibo pérdidas de memory en sqlite3_open aunque sqlite_close, sqlite3_finalize se aplican, por favor, guíame dónde estoy yendo mal. El proyecto está en No – ARC.

-(BOOL)saveMedia:(NSDictionary *)details Download:(NSInteger)completed { //NSLog(@"media savemedia %@",[details objectForKey:@"type"]); BOOL saved = FALSE; NSInteger exists = [self findMedia:[details objectForKey:@"media_id"] playlist_id:[details objectForKey:@"playlist_id"] type:[details objectForKey:@"type"]]; sqlite3_stmt *statement; self.databasePath = [self getDBPath]; const char *dbpath = [databasePath UTF8String]; if (sqlite3_open(dbpath, &wazzupco) == SQLITE_OK) { const char *query_stmt; if (exists == 0) { query_stmt = "INSERT INTO media (media_id, title, description, file, views, thumbnail, version, playlist, playlist_id, author, created_at, type, playlist_created, timeout, playlist_order, media_order, playlist_promo_text, playlist_promo_url, playlist_promo_img,video_promo_text, video_promo_url, video_promo_img, dev_id, device_id, downloaded,slide_timeout) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; } else if([[details objectForKey:@"version"] integerValue] > exists) { NSString *querySQL = [NSString stringWithFormat:@"UPDATE %@ SET media_id=?, title=?, description=?, file=?, views=?, thumbnail=?, version=?, playlist=?, playlist_id=?, author=?, created_at=?, type=?, playlist_created=?, timeout=?, playlist_order=?, media_order=?, playlist_promo_text=?, playlist_promo_url=?, playlist_promo_img=?,video_promo_text=?, video_promo_url=?, video_promo_img=?, dev_id=?, device_id=?, downloaded=?, slide_timeout=? WHERE media_id='%@' AND playlist_id='%@' AND type='%@'", TABLE_MEDIA, [details objectForKey:@"media_id"], [details objectForKey:@"playlist_id"], [details objectForKey:@"type"]]; query_stmt = [querySQL UTF8String]; } else { //to make sure we won't update the database entry unless its a newer version return FALSE; } sqlite3_prepare_v2(wazzupco, query_stmt, -1, &statement, NULL); sqlite3_bind_text(statement, 1, [[details objectForKey:@"media_id"] UTF8String], -1, NULL); sqlite3_bind_text(statement, 2, [[details objectForKey:@"title"] UTF8String], -1, NULL); sqlite3_bind_text(statement, 3, [[details objectForKey:@"description"] UTF8String], -1, NULL); sqlite3_bind_text(statement, 4, [[details objectForKey:@"file"] UTF8String], -1, NULL); sqlite3_bind_text(statement, 5, [[details objectForKey:@"views"] UTF8String], -1, NULL); sqlite3_bind_text(statement, 6, [[details objectForKey:@"thumbnail"] UTF8String], -1, NULL); sqlite3_bind_text(statement, 7, [[details objectForKey:@"version"] UTF8String], -1, NULL); sqlite3_bind_text(statement, 8, [[details objectForKey:@"playlist"] UTF8String], -1, NULL); sqlite3_bind_text(statement, 9, [[details objectForKey:@"playlist_id"] UTF8String], -1, NULL); sqlite3_bind_text(statement, 10, [[details objectForKey:@"author"] UTF8String], -1, NULL); sqlite3_bind_text(statement, 11, [[details objectForKey:@"created_at"] UTF8String], -1, NULL); sqlite3_bind_text(statement, 12, [[details objectForKey:@"type"] UTF8String], -1, NULL); sqlite3_bind_text(statement, 13, [[details objectForKey:@"playlist_created"] UTF8String], -1, NULL); sqlite3_bind_text(statement, 14, [[details objectForKey:@"timeout"] UTF8String], -1, NULL); sqlite3_bind_int(statement, 15, [[details objectForKey:@"playlist_order"] intValue]); sqlite3_bind_int(statement, 16, [[details objectForKey:@"media_order"] intValue]); sqlite3_bind_text(statement, 17, [[details objectForKey:@"playlist_promo_text"] UTF8String], -1, NULL); sqlite3_bind_text(statement, 18, [[details objectForKey:@"playlist_promo_url"] UTF8String], -1, NULL); sqlite3_bind_text(statement, 19, [[details objectForKey:@"playlist_promo_img"] UTF8String], -1, NULL); sqlite3_bind_text(statement, 20, [[details objectForKey:@"video_promo_text"] UTF8String], -1, NULL); sqlite3_bind_text(statement, 21, [[details objectForKey:@"video_promo_url"] UTF8String], -1, NULL); sqlite3_bind_text(statement, 22, [[details objectForKey:@"video_promo_img"] UTF8String], -1, NULL); sqlite3_bind_int(statement, 23, [[details objectForKey:@"dev_id"] intValue]); sqlite3_bind_text(statement, 24, [[details objectForKey:@"device_id"] UTF8String], -1, NULL); sqlite3_bind_int(statement, 25, (int)completed); sqlite3_bind_text(statement, 26, [[details objectForKey:@"slide_timeout"] UTF8String], -1, NULL); if (sqlite3_step(statement) == SQLITE_DONE) { NSLog(@"media added. type:%@",[details objectForKey:@"type"]); saved = TRUE; } sqlite3_finalize(statement); //sqlite3_free(statement); //sqlite3_reset(statement); } sqlite3_close(wazzupco); if (completed != 0 && saved) { [self updateMediaStatus:[details objectForKey:@"file"] Download:1]; } return saved; } 

En el código anterior estoy insertando o actualizando datos de una tabla NSdictionary a sqlite considerando si los datos ya existen o no. El método funciona bien pero produce graves pérdidas de memory (el método se llama varias veces desde un ciclo), cuando está marcado en el Instrumento, se muestra que la fuga está en if (sqlite3_open (dbpath, & wazzupco) == SQLITE_OK).

Del instrumento

Objeto filtrado: Malloc 64Bytes

18

Tamaño de la dirección: 1.12 KB Biblioteca responsable: libsqlite3.dylib Marco responsable: 0x34bdce30

Parece que la pérdida de memory en el código anterior ocurre cuando se ejecuta el caso "else".

 if (sqlite3_open(dbpath, &wazzupco) == SQLITE_OK) { const char *query_stmt; if (exists == 0) { query_stmt = "INSERT INTO media (media_id, title, description, file, views, thumbnail, version, playlist, playlist_id, author, created_at, type, playlist_created, timeout, playlist_order, media_order, playlist_promo_text, playlist_promo_url, playlist_promo_img,video_promo_text, video_promo_url, video_promo_img, dev_id, device_id, downloaded,slide_timeout) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; } else if([[details objectForKey:@"version"] integerValue] > exists) { NSString *querySQL = [NSString stringWithFormat:@"UPDATE %@ SET media_id=?, title=?, description=?, file=?, views=?, thumbnail=?, version=?, playlist=?, playlist_id=?, author=?, created_at=?, type=?, playlist_created=?, timeout=?, playlist_order=?, media_order=?, playlist_promo_text=?, playlist_promo_url=?, playlist_promo_img=?,video_promo_text=?, video_promo_url=?, video_promo_img=?, dev_id=?, device_id=?, downloaded=?, slide_timeout=? WHERE media_id='%@' AND playlist_id='%@' AND type='%@'", TABLE_MEDIA, [details objectForKey:@"media_id"], [details objectForKey:@"playlist_id"], [details objectForKey:@"type"]]; query_stmt = [querySQL UTF8String]; } else { //to make sure we won't update the database entry unless its a newer version return FALSE; } 

Cuando se activa el caso else, sqlite3_close () no se alcanza y esto provocará una pérdida de memory en sqlite3_open

Lo siento, no estoy familiarizado con estos proyectos de SQL Lite y no ARC. Después de search en Google, encontré esta solución para You. Lo que debería considerar convertir el proyecto a ARC. ARC se encargará de la gestión de la memory. Cuando ARC está activado, el comstackdor insertá las instrucciones de administración de memory apropiadas, como retener y liberar posts. Lo mejor es usar ARC ya que el comstackdor tiene una mejor idea del ciclo de vida de un object y es less propenso al error humano.

para convertir el proyecto no ARC en uno habilitado para ARC. Este website puede ayudarlo a Habilitar ARC.