Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 目标C-即使应用了sqlite3\u close(),sqlite3\u打开内存泄漏_Ios_Objective C_Iphone_Sqlite_Memory Leaks - Fatal编程技术网

Ios 目标C-即使应用了sqlite3\u close(),sqlite3\u打开内存泄漏

Ios 目标C-即使应用了sqlite3\u close(),sqlite3\u打开内存泄漏,ios,objective-c,iphone,sqlite,memory-leaks,Ios,Objective C,Iphone,Sqlite,Memory Leaks,即使应用了sqlite\u close、sqlite3\u finalize,我仍在sqlite3\u open处发现内存泄漏,请指导我哪里出了问题。项目处于非圆弧状态 -(BOOL)saveMedia:(NSDictionary *)details Download:(NSInteger)completed { //NSLog(@"media savemedia %@",[details objectForKey:@"type"]); BOOL saved = FAL

即使应用了sqlite\u close、sqlite3\u finalize,我仍在sqlite3\u open处发现内存泄漏,请指导我哪里出了问题。项目处于非圆弧状态

     -(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表。该方法执行得很好,但会产生严重的内存泄漏(该方法从循环中多次调用),当检入仪器时,它显示泄漏处于if(sqlite3_open(dbpath,&wazzupco)=SQLITE_OK)

从仪器

泄漏对象:Malloc 64Bytes

18 地址 大小:1.12KB 责任库:libsqlite3.dylib
负责框架:0x34 bdCE30

< p>对不起,我不熟悉这些SQL Lite和非ARC项目,在一些Google之后,我为您找到了这个解决方案。我想您应该考虑将项目转换为ARC。ARC将为您处理内存管理。启用ARC时,编译器将插入适当的内存管理语句,如retain和release消息。最好使用ARC,因为编译器对对象的生命周期有更好的了解,并且不太容易出现人为错误


将非圆弧项目转换为启用圆弧的项目。此网站可能会帮助您

执行“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出现内存泄漏

谢谢Sarath,但此时我无法将项目转换为ARC,因为使用了许多库,转换将很困难(ok:)。希望有人能给出正确的答案为什么要忽略
sqlite3\u prepare\u v2()
的返回代码?@CL.我是否需要添加一个条件,if(sqlite3\u prepare\u v2(wazzupco,query\u stmt,-1,&statement,NULL))并在其中包含代码块。你能解释一下吗;否则,你不会注意到错误(或者只是看到你的应用程序在稍后爆炸)。@CL.谢谢;我没有意识到这一点,我已经更新了我的代码。