bad :: alloc在libmysqlcppconn的get_driver_instance()函数中

时间:2018-07-30 08:42:34

标签: c++ mysql

我有以下可以在Ubuntu 14.04.5上编译的C ++程序

#include <cppconn/driver.h>
#include <iostream>

int main(void)
{
        sql::Driver *driver;
        sql::Connection *con;
        const sql::SQLString host = "tcp://127.0.0.1:3306";
        const sql::SQLString user = "user";
        const sql::SQLString pass = "pass";
        std::cout << "Before driver" << std::endl;
        driver = get_driver_instance();
        std::cout << "before conn" << std::endl;
        con = driver->connect(host, user, pass);
        return 0;
}

当我在ubuntu 14.04.5上运行此代码时,我得到了预期的输出

angelluis@angelluis14.04$ ./mysql_test 
before driver 
before conn

但是当我在Ubuntu 16.04.3上运行该程序时,得到以下输出

angelluis@angelluis16.04$ ./mysql_test 
before driver 
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
Abortado

gdb回溯输出为

#0  0x00007ffff7187428 in __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:54
#1  0x00007ffff718902a in __GI_abort () at abort.c:89
#2  0x00007ffff77c184d in __gnu_cxx::__verbose_terminate_handler() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3  0x00007ffff77bf6b6 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#4  0x00007ffff77bf701 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5  0x00007ffff77bf919 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6  0x00007ffff77bfebc in operator new(unsigned long) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#7  0x00007ffff785396d in void std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct<char*>(char*, char*, std::forward_iterator_tag) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#8  0x00007ffff78539cf in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#9  0x00007ffff7b31ce9 in sql::SQLString::SQLString(sql::SQLString const&) () from /usr/lib/libmysqlcppconn.so.7
#10 0x00007ffff7b494d1 in std::pair<sql::SQLString const, boost::shared_ptr<sql::mysql::MySQL_Driver> >::pair(sql::SQLString const&, boost::shared_ptr<sql::mysql::MySQL_Driver> const&) () from /usr/lib/libmysqlcppconn.so.7
#11 0x00007ffff7b48f00 in std::map<sql::SQLString, boost::shared_ptr<sql::mysql::MySQL_Driver>, std::less<sql::SQLString>, std::allocator<std::pair<sql::SQLString const, boost::shared_ptr<sql::mysql::MySQL_Driver> > > >::operator[](sql::SQLString const&) () from /usr/lib/libmysqlcppconn.so.7
#12 0x00007ffff7b483dd in sql::mysql::get_driver_instance_by_name(char const*) () from /usr/lib/libmysqlcppconn.so.7
#13 0x00007ffff7b482b6 in sql::mysql::get_driver_instance() () from /usr/lib/libmysqlcppconn.so.7
#14 0x00007ffff7b482a4 in get_driver_instance () from /usr/lib/libmysqlcppconn.so.7
#15 0x0000000000400cf3 in main ()

似乎问题出在get_driver_instance()函数上(如gdb所说),但是如果我从程序中注释con = driver->connect(host, user, pass);时,它在两个Ubuntu上都可以正常工作(因此,我不知道问题出在哪里, gdb说get_driver_instance(),但我的测试表明问题出在con = driver->connect(host, user, pass);)。

程序使用以下命令编译:

angelluis@angelluis14.04$ g++ mysql_test.cpp -lmysqlcppconn -o mysql_test

ubuntu 14.04上的链接库为:

angelluis@angelluis14.04$ ldd mysql_test 
    linux-vdso.so.1 =>  (0x00007ffff1b4c000)
    libmysqlcppconn.so.7 => /usr/lib/libmysqlcppconn.so.7 (0x00007eff6e85c000)
    libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007eff6e546000)
    libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007eff6e32e000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007eff6df65000)
    libmysqlclient.so.18 => /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18 (0x00007eff6da2c000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007eff6d726000)
    /lib64/ld-linux-x86-64.so.2 (0x00007eff6eb6c000)
    libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007eff6d50d000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007eff6d309000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007eff6d0eb000)

虽然ubuntu 16.04上的链接库为:

angelluis@angelluis16.04$ ldd mysql_test 
    linux-vdso.so.1 =>  (0x00007ffcd6df1000)
    libmysqlcppconn.so.7 => /usr/lib/libmysqlcppconn.so.7 (0x00007f2b3732c000)
    libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f2b36faa000)
    libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f2b36d94000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f2b369c9000)
    libmysqlclient.so.20 => /usr/lib/x86_64-linux-gnu/libmysqlclient.so.20 (0x00007f2b363ba000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f2b360b1000)
    /lib64/ld-linux-x86-64.so.2 (0x0000560afa1d1000)
    libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f2b35e96000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f2b35c92000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f2b35a75000)

如何解决此问题? 谢谢

0 个答案:

没有答案