C ++ Poco MySQL数据包装器分段错误错误

时间:2019-03-14 14:07:39

标签: c++ poco-libraries

我正在使用Poco 1.8数据库。我想构建一个数据库包装程序来同时运行多个SQL查询。

我用以下内容构建一个dbconn.h。

    std::string _dbConnString = "host=" + host + ";user=" + user + ";password=" + password + ";db="+db
    +";compress=true;auto-reconnect=true";


    std::string getkey(int userID, int exchangeID) {
        Poco::Data::MySQL::Connector::registerConnector();

        std::string key;

        {
            // 0. connect & create table
            Session session(Poco::Data::MySQL::Connector::KEY,_dbConnString);



            Statement selectn(session);
            selectn << "SELECT key FROM API WHERE user_id=? ", into(key), use(userID),  now;

        }
        catch (const Poco::Data::ConnectionFailedException &ce)
        {
            std::cout << ce.displayText() << std::endl;
        }
        catch (const Poco::Data::MySQL::StatementException &se)
        {
            std::cout << se.displayText() << std::endl;
        }


        Poco::Data::MySQL::Connector::unregisterConnector();

        return key;

    }

 std::string getuser(int userID, int exchangeID) {
        Poco::Data::MySQL::Connector::registerConnector();

        std::string user;
    try
        {
            // 0. connect & create table
            Session session(Poco::Data::MySQL::Connector::KEY, _dbConnString);



            Statement selectn(session);
            selectn << "SELECT user FROM API WHERE user_id=? ", into(user), use(userID),  now;

        }
        catch (const Poco::Data::ConnectionFailedException &ce)
        {
            std::cout << ce.displayText() << std::endl;
        }
        catch (const Poco::Data::MySQL::StatementException &se)
        {
            std::cout << se.displayText() << std::endl;
        }


        Poco::Data::MySQL::Connector::unregisterConnector();

        return user;

    }

以下将抛出分段错误。 teset.cpp

int user_id = 100;
int exchange_id = 1;
std::string key = getkey(user_id,exchange_id);  //no error
std::string user = getuser(user_id,exchange_id);  //segmentation fault

如果您颠倒了函数调用的顺序,则会出现相同的错误:

std::string user = getuser(user_id,exchange_id);  //no error
std::string key = getkey(user_id,exchange_id);  //segmentation fault

我认为与创建会话有关。但是,互联网上没有太多Poco数据示例,请多多指教。

Valgrind错误:

==3316== Thread 2:
==3316== Invalid read of size 4
==3316==    at 0x4E45FA0: pthread_mutex_lock (pthread_mutex_lock.c:65)
==3316==    by 0x76332D9: ??? (in /usr/lib/x86_64-linux-gnu/libmysqlclient.so.20.3.12)
==3316==    by 0x760AB40: ??? (in /usr/lib/x86_64-linux-gnu/libmysqlclient.so.20.3.12)
==3316==    by 0x75DEB2A: mysql_real_connect (in /usr/lib/x86_64-linux-gnu/libmysqlclient.so.20.3.12)
==3316==    by 0x5B43736: Poco::Data::MySQL::SessionHandle::connect(char const*, char const*, char const*, char const*, unsigned int) (in /usr/lib/libPocoDataMySQL.so.50)
==3316==    by 0x5B4795A: Poco::Data::MySQL::SessionImpl::open(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (in /usr/lib/libPocoDataMySQL.so.50)
==3316==    by 0x5B48D86: Poco::Data::MySQL::SessionImpl::SessionImpl(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned long) (in /usr/lib/libPocoDataMySQL.so.50)
==3316==    by 0x5B3E51D: Poco::Data::MySQL::Connector::createSession(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned long) (in /usr/lib/libPocoDataMySQL.so.50)
==3316==    by 0x5498B83: Poco::Data::SessionFactory::create(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned long) (in /usr/lib/libPocoData.so.50)
==3316==    by 0x115048: getAPIsecret[abi:cxx11](int, int) (dbconnector.h:85)
==3316==    by 0x11561A: Trading::Binance::BUY::process(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (binance.cpp:36)
==3316==    by 0x110F95: Trading::ExchangeRequest::run() (exchangeRequest.cpp:58)
==3316==  Address 0x30 is not stack'd, malloc'd or (recently) free'd
==3316== 
==3316== 
==3316== Process terminating with default action of signal 11 (SIGSEGV)
==3316==  Access not within mapped region at address 0x30
==3316==    at 0x4E45FA0: pthread_mutex_lock (pthread_mutex_lock.c:65)
==3316==    by 0x76332D9: ??? (in /usr/lib/x86_64-linux-gnu/libmysqlclient.so.20.3.12)
==3316==    by 0x760AB40: ??? (in /usr/lib/x86_64-linux-gnu/libmysqlclient.so.20.3.12)
==3316==    by 0x75DEB2A: mysql_real_connect (in /usr/lib/x86_64-linux-gnu/libmysqlclient.so.20.3.12)
==3316==    by 0x5B43736: Poco::Data::MySQL::SessionHandle::connect(char const*, char const*, char const*, char const*, unsigned int) (in /usr/lib/libPocoDataMySQL.so.50)
==3316==    by 0x5B4795A: Poco::Data::MySQL::SessionImpl::open(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (in /usr/lib/libPocoDataMySQL.so.50)
==3316==    by 0x5B48D86: Poco::Data::MySQL::SessionImpl::SessionImpl(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned long) (in /usr/lib/libPocoDataMySQL.so.50)
==3316==    by 0x5B3E51D: Poco::Data::MySQL::Connector::createSession(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned long) (in /usr/lib/libPocoDataMySQL.so.50)
==3316==    by 0x5498B83: Poco::Data::SessionFactory::create(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned long) (in /usr/lib/libPocoData.so.50)
==3316==    by 0x115048: getAPIsecret[abi:cxx11](int, int) (dbconnector.h:85)
==3316==    by 0x11561A: Trading::Binance::BUY::process(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (binance.cpp:36)
==3316==    by 0x110F95: Trading::ExchangeRequest::run() (exchangeRequest.cpp:58)
==3316==  If you believe this happened as a result of a stack
==3316==  overflow in your program's main thread (unlikely but
==3316==  possible), you can try to increase the size of the
==3316==  main thread stack using the --main-stacksize= flag.
==3316==  The main thread stack size used in this run was 8388608.
==3316== 
==3316== HEAP SUMMARY:
==3316==     in use at exit: 266,548 bytes in 966 blocks
==3316==   total heap usage: 2,241 allocs, 1,275 frees, 790,945 bytes allocated
==3316== 
==3316== LEAK SUMMARY:
==3316==    definitely lost: 0 bytes in 0 blocks
==3316==    indirectly lost: 0 bytes in 0 blocks
==3316==      possibly lost: 62,545 bytes in 63 blocks
==3316==    still reachable: 204,003 bytes in 903 blocks
==3316==         suppressed: 0 bytes in 0 blocks
==3316== Rerun with --leak-check=full to see details of leaked memory
==3316== 
==3316== For counts of detected and suppressed errors, rerun with: -v
==3316== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

1 个答案:

答案 0 :(得分:0)

Poco::Data::MySQL::Connector::registerConnector();Poco::Data::MySQL::Connector::unregisterConnector();具有非局部效果,您应该将它们从同时调用的位中移出,以进行某些全局设置/拆卸。只需调用一次就足够了,您不必为每个查询重复它们。