ODB与PostgreSQL bad_alloc异常

时间:2016-01-29 01:57:36

标签: c++ postgresql dll qt-creator odb

我试图在我的项目中使用ODB。因此,我使用VS2015为他们的odb和odb-pgsql库编译windows源代码,选择Release和x64配置。我链接了我的标准postgres安装库(预构建的二进制文件64位)。

我的项目由cmake和QtCreator管理。以下是我使用的套件中的编译器设置。

kit configuation in QtCreator

我设置了一个非常简单的项目来复制错误。这是CMakeLists.txt

project(minimal_odb CXX)
INCLUDE_DIRECTORIES(
  "C:/Users/micha/Downloads/libodb-2.4.0/libodb-2.4.0/"
  "C:/Users/micha/Downloads/libodb-pgsql-2.4.0/libodb-pgsql-2.4.0/")
add_executable(exec main.cpp)
target_link_libraries(exec
  "C:/Users/micha/Downloads/libodb-2.4.0/libodb-2.4.0/lib64/odb.lib"
  "C:/Users/micha/Downloads/libodb-pgsql-2.4.0/libodb-pgsql-2.4.0/lib64/odb-pgsql.lib")

运行cmake将打印使用过的编译器。

-- The CXX compiler identification is MSVC 19.0.23506.0
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done

这里是main.cpp

#include "odb/pgsql/database.hxx"

int main() {
  const std::string pg = "postgres";
  ::odb::pgsql::database db(pg, pg, pg, pg, 0);
  return 0;
}

在完成编制期间,我会收到令人困惑的警告。

compiler warnings

在运行时,可以从修改后的路径environemt

中找到DLL
C:\Users\micha\Downloads\libodb-2.4.0\libodb-2.4.0\bin64;
C:\Users\micha\Downloads\libodb-pgsql-2.4.0\libodb-pgsql-2.4.0\bin64

但是我的程序会立即以bad_alloc异常终止。我调试了应用程序,这是我的调用堆栈。

callstack during debugging

当字符串从user_复制到本地::odb::pgsql::database变量时,就会发生错误。

如果我更深入地进行复制赋值,我可以看到来自main的手动字符串并未真正设置并指向0x0从内存中读取随机值。

stack values in assign operation

应用程序将以下内容打印到我的终端。

Exception at 0x7ffd148d1f08, code: 0xe06d7363: C++ exception, flags=0x1 (execution cannot be continued) (first chance) at f:\dd\vctools\crt\vcstartup\src\heap\throw_bad_alloc.cpp:33

Exception at 0x7ffd148d1f08, code: 0xe06d7363: C++ exception, flags=0x1 (execution cannot be continued) (first chance) at f:\dd\vctools\crt\vcstartup\src\heap\throw_bad_alloc.cpp:33

Exception at 0x7ffd148d1f08, code: 0xe06d7363: C++ exception, flags=0x1 (execution cannot be continued) (first chance) at c:\program files (x86)\microsoft visual studio 14.0\vc\include\xstring:2185

Exception at 0x7ffd148d1f08, code: 0xe06d7363: C++ exception, flags=0x1 (execution cannot be continued) (first chance) in ntdll!RcConsolidateFrames

Exception at 0x7ffd148d1f08, code: 0xe06d7363: C++ exception, flags=0x1 (execution cannot be continued) in ntdll!RcConsolidateFrames

我不奇怪得到bad_alloc这个_Num值,但我想知道为什么我的字符串搞砸了。我尝试了一些不同的编译器,如amd64和编译的odb,odb-pgsql使用UNICODE和Multi-Byte字符集,但没有任何帮助。至少使用amd64我没有得到上面提到的前5个警告。

1 个答案:

答案 0 :(得分:0)

const std::string pg = "postgres";更改为std::string pg = "postgres";是否有帮助?

否则它看起来像链接问题。(如果没有帮助,将编辑/删除答案)