HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – Objective C – sqlite3_open即使应用了sqlite3_close(),内存也会泄漏大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
即使应用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],null);
        sqlite3_bind_text(statement,2,[[details objectForKey:@"title"] UTF8String],3,[[details objectForKey:@"description"] UTF8String],4,[[details objectForKey:@"file"] UTF8String],5,[[details objectForKey:@"views"] UTF8String],6,[[details objectForKey:@"thumbnail"] UTF8String],7,[[details objectForKey:@"version"] UTF8String],8,[[details objectForKey:@"playlist"] UTF8String],9,[[details objectForKey:@"playlist_id"] UTF8String],10,[[details objectForKey:@"author"] UTF8String],11,[[details objectForKey:@"created_at"] UTF8String],12,[[details objectForKey:@"type"] UTF8String],13,[[details objectForKey:@"playlist_created"] UTF8String],14,[[details objectForKey:@"timeout"] UTF8String],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],18,[[details objectForKey:@"playlist_promo_url"] UTF8String],19,[[details objectForKey:@"playlist_promo_img"] UTF8String],20,[[details objectForKey:@"video_promo_text"] UTF8String],21,[[details objectForKey:@"video_promo_url"] UTF8String],22,[[details objectForKey:@"video_promo_img"] UTF8String],23,[[details objectForKey:@"dev_id"] intValue]);
        sqlite3_bind_text(statement,24,[[details objectForKey:@"device_id"] UTF8String],25,(int)completed);
        sqlite3_bind_text(statement,26,[[details objectForKey:@"slide_timeout"] UTF8String],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,?)";
    }
    else if([[details objectForKey:@"version"] IntegerValue] > exists)
    {
        NSString *querysql = [NSString StringWithFormat:@"updatE %@ SET media_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_open即使应用了sqlite3_close(),内存也会泄漏全部内容,希望文章能够帮你解决ios – Objective C – sqlite3_open即使应用了sqlite3_close(),内存也会泄漏所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。