c++ - SQLite 注册 VFS 导致段错误

标签 c++ sqlite

我正在尝试注册 VFS在 SQLite 中:

#include <iostream>
#include "sqlite3.h"

int vfs_open(sqlite3_vfs* vfs, const char *zName, sqlite3_file* file, int flags, int *pOutFlags)
{

  std::cout << "Hello open";
  return SQLITE_OK;
}
int vfs_access(sqlite3_vfs* vfs, const char *zName, int flags, int *pResOut)
{
  std::cout << "Hello access";
  return 0;
}

int main () {
  static sqlite3_vfs vfs;
  vfs.iVersion = 1;
  vfs.szOsFile = 1000;
  vfs.zName = "foo";
  vfs.xOpen = vfs_open;
  vfs.xAccess = vfs_access;

  sqlite3_vfs_register(&vfs, 0);

  sqlite3 *db;
  sqlite3_open_v2("db.db", &db, SQLITE_OPEN_READONLY, "foo");
}

但是当我运行这段代码时,我得到了段错误:

$ g++ test.cc libsqlite3.dylib 
$ ./a.out
Segmentation fault: 11

对我做错了什么有什么帮助吗?

最佳答案

sqlite3_vfs 结构有很多函数的指针。我找不到任何关于您必须定义的最少函数集的文档,因此假设它们都是必需的可能更安全,即使只是作为 stub 函数。

在您的情况下,我猜 sqlite 正在调用您尚未定义的函数之一(可能是 xFullPathname),导致尝试在 NULL 上运行函数。

关于c++ - SQLite 注册 VFS 导致段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42964641/

相关文章:

c++ - 前向声明类型上的 std::vector

ios - 在 swift 中使用 sqlite 数据库以字符串形式检索数据时,用整数对列进行排序?

c++ - 成对生成列表

c++ - 如何在 CLion 上使用 Allegro?

c++ - 解析文件头

sqlite - “Library Routine Called Out Of Sequence” sqlite3_prepare_v2(创建表)

iphone - 当我在运行的 iPhone 应用程序中第二次从 sqlite 数据库中检索时,我的应用程序崩溃了

c++ - 如何在调用(未编译)某个函数时触发警告?

.net - RESTful Web服务的SQLite

支持外键的 SQLite 工具?