• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - iOS 内存问题中的 Sqlite

[复制链接]
菜鸟教程小白 发表于 2022-12-13 09:27:43 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我花了几个小时试图解决这个问题,但我刚刚放弃了;我不知道出了什么问题。

在我的应用程序中,我执行嵌套 SQL 操作来正确设置我的所有对象。由于某种原因,有时 sqlite3 对象无法正确释放,导致内存飙升。我知道正确使用 sql3_close 和 sql3_finalize 是一个问题。但是,正如您将看到的,我认为我正确地使用了它们。

这是问题的根源:

- (NSArray *) getAllSessions {
    if (sqlite3_open(dbPath, &db) == SQLITE_OK) { 
        if (sqlite3_prepare_v2(db, query_stmt, -1, &statement, NULL) == SQLITE_OK) {
            while (sqlite3_step(statement) == SQLITE_ROW) {

                //I found out that doing something like that
                //toAdd.in_loc = [self getIndoorLocationWithId:[NSNumber numberWithIntint)sqlite3_column_int(statement, 6)]];
                //messes the memory up all the time
                //but doing that works OK:
                NSNumber *_id = [[NSNumber alloc]  initWithIntint) sqlite3_column_int(statement, 5)];
                toAdd.out_loc = [self getOutdoorLocationWithId:_id];
                [_id release];

                //So I did the same with the second one, but this one messes memory up:
                NSNumber *id2 = [[NSNumber alloc] initWithIntint)sqlite3_column_int(statement, 6)];
                toAdd.in_loc = [self getIndoorLocationWithId:id2];
                [id2 release];
            }
            sqlite3_finalize(statement);
        }
        sqlite3_close(db);
    } 
}

所以这里是搞乱内存的一个:

- (IndoorLocation *) getIndoorLocationWithIdNSNumber *) locId {
    if (sqlite3_open([databasePath UTF8String], &db) == SQLITE_OK) { 
        if (sqlite3_prepare_v2(db, query_stmt, -1, &statement, NULL) == SQLITE_OK) {
            while (sqlite3_step(statement) == SQLITE_ROW) {  
                //if I comment the thing below it works
                NSNumber *_id = [[NSNumber alloc]  initWithIntint) sqlite3_column_int(statement, 5)];
                toReturn.outLoc = [self getOutdoorLocationWithId:_id];
                [_id release];

            }
            sqlite3_finalize(statement);
        }
        sqlite3_close(db);
    }
}

所以在搞砸内存的那个中,我使用了与第一次完全相同的函数(getOutdoorLocationwithId),以同样的方式但它不起作用,sqlite3 对象没有被正确释放。

我希望你能理解我的问题,这让我发疯了!



Best Answer-推荐答案


在这样的单用户应用中,无需不断打开和关闭。这与基于服务器的应用程序不同,在该应用程序中,您与多个用户建立连接池,而保持连接会破坏池并损害可伸缩性。在手机应用程序中打开它并保持打开状态。完成后关闭它。至少,在递归调用中执行此操作。

更糟糕的是,你在递归调用中打开和关闭 - 让它保持打开状态。

还有:

  • 你没有检查 finalize 的返回码
  • 你没有检查 close 的返回码。
  • 您正在准备(编译)语句,但没有保存它们 - 对保存的语句调用 reset 并再次执行它。

考虑使用 FMDB - 它是一个很好的包装器。

顺便说一句,这里有一个更丰富、更持久的关闭,但不要在每次调用时都使用它。当你结束或你的应用程序进入后台时关闭它......这是我的,类似于 fmdb 所做的。

- (void)close
{
    if (_sqlite3)
    {
        ENInfo(@"closing");
        [self clearStatementCache];

        int rc = sqlite3_close(_sqlite3);
        ENDebug(@"close rc=%d", rc);

        if (rc == SQLITE_BUSY) 
        { 
            ENError(@"SQLITE_BUSY: not all statements cleanly finalized");

            sqlite3_stmt *stmt; 
            while ((stmt = sqlite3_next_stmt(_sqlite3, 0x00)) != 0) 
            {
                ENDebug(@"finalizing stmt");
                sqlite3_finalize(stmt); 
            }

            rc = sqlite3_close(_sqlite3);
        }

        if (rc != SQLITE_OK)
        {
            ENError(@"close not OK.  rc=%d", rc);
        }

        _sqlite3 = NULL;
    }
}

关于ios - iOS 内存问题中的 Sqlite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7762591/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap