3
votes

While trying to connect to a mysql database using c++ code I'm having a segmentation fault when I call connect.

This is the code:

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string.h>
#include "mysql_connection.h"
#include "mysql_driver.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/connection.h>
#include <cppconn/prepared_statement.h>
#include "iolib.h"

int main(int argc, char* argv[])
{
    cout<<"1\n";
    sql::Driver *driver = sql::mysql::get_driver_instance();
    cout<<"Driver: "<<driver<<", "<<driver->getName()<<"\n";
    sql::Connection *conn = driver->connect("tcp://<ip>:3306", "root", "password");
    cout<<"Connected\n";
    return 0;
}

The output is:

1
Driver: 0x1755050, MySQL Connector C++ (libmysql)
Segmentation fault (core dumped)

Previously I forgot to add this machine to the allowed IPs, then it gave me an error saying I wasn't authorized instead of segmentation fault. After I allowed the IP I get this error. I can block the IP to get the not allowed IP error again.

This is the makefile:

CC=g++
CFLAGS=-c -g -rdynamic
LDFLAGS=-lpthread -ldl -rdynamic -lmysqlcppconn
EXECUTABLE=myexe

all: main
    $(CC) $(LDFLAGS) *.o -o $(EXECUTABLE)
    @echo -e "\033[32mFinished!\033[0m"

main:
    $(CC) $(CFLAGS) -o iolib.o iolib.cpp
    $(CC) $(CFLAGS) -o importer.o importer.cpp
    @echo -e "\033[36mObject files compiled!\033[0m"

This machine is running Fedora 19 and the libraries were installed with "yum install mysql mysql-connector-c++-devel mysql++". I may be off a tad bit with the exact commands here but important to say they were all installed with yum.

Searching the web for similar problems, the most likely thing I've come across are people saying that there must be a problem with my libraries so ldd myexe follows.

    linux-vdso.so.1 =>  (0x00007fff72e6b000)
    libpthread.so.0 => /lib64/libpthread.so.0 (0x000000370f000000)
    libdl.so.2 => /lib64/libdl.so.2 (0x000000370ec00000)
    libmysqlcppconn.so.6 => /lib64/libmysqlcppconn.so.6 (0x00007f2c335ee000)
    libstdc++.so.6 => /lib64/libstdc++.so.6 (0x0000003716800000)
    libm.so.6 => /lib64/libm.so.6 (0x000000370f800000)
    libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x0000003710800000)
    libc.so.6 => /lib64/libc.so.6 (0x000000370e800000)
    /lib64/ld-linux-x86-64.so.2 (0x000000370e400000)
    libmysqlclient.so.18 => /usr/lib64/mysql/libmysqlclient.so.18 (0x00007f2c3301d000)
    libz.so.1 => /lib64/libz.so.1 (0x000000370f400000)
    libssl.so.10 => /lib64/libssl.so.10 (0x0000003728800000)
    libcrypto.so.10 => /lib64/libcrypto.so.10 (0x0000003723400000)
    libgssapi_krb5.so.2 => /lib64/libgssapi_krb5.so.2 (0x0000003724c00000)
    libkrb5.so.3 => /lib64/libkrb5.so.3 (0x0000003725800000)
    libcom_err.so.2 => /lib64/libcom_err.so.2 (0x0000003723c00000)
    libk5crypto.so.3 => /lib64/libk5crypto.so.3 (0x0000003725400000)
    libkrb5support.so.0 => /lib64/libkrb5support.so.0 (0x0000003725000000)
    libkeyutils.so.1 => /lib64/libkeyutils.so.1 (0x0000003724800000)
    libresolv.so.2 => /lib64/libresolv.so.2 (0x0000003711000000)
    libselinux.so.1 => /lib64/libselinux.so.1 (0x0000003710400000)
    libpcre.so.1 => /lib64/libpcre.so.1 (0x0000003710000000)

How can I make this work?

I appreciate any help.

1
Why not debug the core file and see what function is causing the crash?Noam Rathaus
I just have access to the headers. driver->connect executes code inside the lib.Allan
The error you gave states: Segmentation fault (core dumped) so the core is present, and you should use it to find out what causes the issueNoam Rathaus

1 Answers

0
votes

Many of the MySQL C++ Connector functions throw exceptions.

Surround your function calls with a try / catch block to catch the exception. When execution goes into the catch block, use your debugger to find out more information from the exception, such as the what() method.

Another alternative is to link with the debug version of the libraries. You may have to download the source and build them yourself.