C ++ Poco MySQL Ошибка ошибки сегментации оболочки данных - PullRequest
2 голосов
/ 14 марта 2019

Я использую библиотеку данных 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 голосов
/ 14 марта 2019

Poco::Data::MySQL::Connector::registerConnector(); и Poco::Data::MySQL::Connector::unregisterConnector(); имеют нелокальные эффекты, вы должны переместить их из одновременно вызываемых битов в какую-то глобальную настройку / демонтаж.И этого должно быть достаточно, чтобы вызвать их один раз, вам не нужно повторять их для каждого запроса.

...