2

I am currently developing a small C++ program that uses a database connection. It is a connection with a MySQL database through CPPCONN connector.

Cause

I am using multiple threads and therefor I have created the following methods:

void Database::startThread()
{
    fDriver->threadInit();
}

void Database::stopThread()
{
    fDriver->threadEnd();
}

void Database::connect(const string & host, const string & user, const string & password, const string & database)
{
        fDriver = sql::mysql::get_driver_instance();
        fConnection.reset(fDriver->connect((SQLString)host,(SQLString)user,(SQLString)password));
        fConnection->setSchema((SQLString) database);
        fStatement.reset(fConnection->createStatement());
        fConnection->setClientOption("multi-queries","true");
        fConnection->setClientOption("multi-statements","true");
}

The problem is that I encounter a segmentation fault at the fDriver->threadInit() call. I can assure you that fDriver is properly instantiated at that point through the connect function. (fDriver is not null either)

The crash

Unfortunately I cannot give much more useful information but this is GDB's backtrace:

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7ffff4d66700 (LWP 16786)]
0x0000000000414547 in Database::startThread (this=Unhandled dwarf expression opcode 0xf3
#0  0x0000000000414547 in Database::startThread (this=Unhandled dwarf expression opcode 0xf3) at src/core/database.cpp:73
#1  0x0000000000405443 in Parser::Parser (this=0x7ffff4d659b8) at src/core/sv_parse.cpp:11
#2  0x000000000041e76d in MessageProcessor::MessageProcessor (this=0x7ffff4d659b0, serverStartTime=...) at src/server/messageProcessor.cpp:12
#3  0x000000000041bae8 in Server::__lambda1::operator() (__closure=0x62c740) at src/server/server.cpp:89
#4  0x00007ffff763f550 in execute_native_thread_routine () at ../../../../../libstdc++-v3/src/c++11/thread.cc:84
#5  0x00007ffff6edb851 in start_thread () from /lib64/libpthread.so.0
#6  0x00007ffff6c2994d in clone () from /lib64/libc.so.6

Remark

Now the weird part: this crash does not occur all the time ! Sometimes it works perfectly. But it is of course extremely annoying if it doesn't. CPPCONN version is 1.1.3 and we are using g++ version 4.8.1.

I hope someone can shed some light on this mystery !

Giriel

1 Answer 1

3

I struggled for hours with the same mysterious segmentation faults. I found that adding mutex lock around get_driver_instance() solves the problem. Here is a basic skeleton for a threaded function. This works for selecting from database, might not work for inserting or updating.

#include <mutex>

std::mutex mtx;

void test() 
{
  sql::Driver *driver;
  sql::Connection *con;

  try {  
    mtx.lock();
    driver = get_driver_instance();
    mtx.unlock();
    driver->threadInit();
    con = driver->connect(HOST, USER, PASS);
    ...
    con->close();
    driver->threadEnd();
  } catch(...) { ... }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.