gpt4 book ai didi

ios - Objective-C - 即使应用了 sqlite3_close(),sqlite3_open 内存泄漏

转载 作者:可可西里 更新时间:2023-11-01 05:43:39 30 4
gpt4 key购买 nike

即使应用了 sqlite_close、sqlite3_finalize,我仍然在 sqlite3_open 发生内存泄漏,请指导我哪里出错了。项目在非 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;
}

在上面的代码中,我通过考虑数据是否已经存在来将数据从 NSdictionary 插入或更新到 sqlite 表。该方法执行良好,但它会产生严重的内存泄漏(该方法从循环中多次调用),在 Instrument 中检查时显示泄漏位于 if (sqlite3_open(dbpath, &wazzupco) == SQLITE_OK)。

来自乐器

泄漏的对象:Malloc 64Bytes

18

地址大小:1.12KB负责库:libsqlite3.dylib负责帧:0x34bdce30

最佳答案

上面代码中的内存泄漏似乎是在执行“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;
}

当触发 else 情况时,未达到 sqlite3_close() 并将导致 sqlite3_open 处的内存泄漏

关于ios - Objective-C - 即使应用了 sqlite3_close(),sqlite3_open 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32221007/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com