4

Using C++ in Xcode I try to access a MySQL database with the MySQL Connector/C++. Problem is that the program (compiled with Xcode) always crashes with

EXC_BAD_ACCESS (code=13, address=0x0)

when calling

driver->connect(url, user, pass)

In Xcode I created a complete new project (OS X > Command Line Tool), inserted the code (see below) in the main.cpp, added Boost and MySQL Connector header include paths as well as libmysqlcppconn.6.1.1.1.dylib as Link Library and hit the Run button.

Next thing is, when I compile the program manually using

c++ -o test -I /usr/local/mysqlConnector/include/ -lmysqlcppconn main.cpp

the program runs fine and also runs the INSERT statement on the table.

The program code is taken from the MySQL Connector/C++ examples, namely the pthreads.cpp example, but truncated to the essential parts:

/* Standard C++ includes */
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <stdexcept>

#include <mysql_connection.h>
#include <mysql_driver.h>

#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>

std::string url;
std::string user;
std::string pass;
std::string database;

/**
 * Usage example for Driver, Connection, (simple) Statement, ResultSet
 */
int main(int argc, const char **argv)
{
    sql::Driver *driver;
    std::auto_ptr< sql::Connection > con;

    url = "tcp://127.0.0.1:3306";
    user = "appserver";
    pass = "testpw";
    database = "appserver";

    try {
        driver = sql::mysql::get_driver_instance();

        /* Using the Driver to create a connection */
        con.reset(driver->connect(url, user, pass));
        con->setSchema(database);

    sql::Statement* stmt = con->createStatement();
    stmt->execute("INSERT INTO testtable (testnumber) values (5)");
    } catch (sql::SQLException &e) {
        return EXIT_FAILURE;
    } catch (std::runtime_error &e) {
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}
Nero
  • 1,295
  • 1
  • 16
  • 23
  • What is the value of `driver` at the point you call `driver->connect(url, user, pass)` ? I.e. Was the call to `driver = sql::mysql::get_driver_instance();` successful? – phonetagger Jan 28 '13 at 20:37
  • The value of `driver` is valid, a call to `std::cout << driver->getMajorVersion() << std::endl;` returns/prints the correct version. – Nero Jan 28 '13 at 20:50
  • The value of `driver` is `0x1005000e0`, so it seems to be valid, or do I misinterpret that? (I used the printf method with void* cast.) – Nero Jan 28 '13 at 21:17
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/23527/discussion-between-nero-and-phonetagger) – Nero Jan 28 '13 at 21:30

2 Answers2

9

Ok, the problem is solved.

Here the problem was one compile flag. The MySQL Connector/C++ was compiled without the
-stdlib=libc++ flag, but Xcode added that compile/link flag to its commands. This caused the crash. This also explains, why the manually compiled program worked, as I didn't include that flag to the compile command.

To make it more clear: I recompiled the MySQL Connector/C++ with the -stdlib=libc++ flag. Then the program compiled by Xcode works fine for me. To compile the MySQL Connector/C++ I added

-DMYSQL_CXXFLAGS=-stdlib=libc++

to the cmake command that needs to be run when installing the connector.

make VERBOSE=1

then proved that the flag is actually used when compiling the connectors source.

Nero
  • 1,295
  • 1
  • 16
  • 23
  • If you feel that this is the answer to the problem, please select it so this question doesn't show up forever in the "unanswered questions" tab. – phonetagger Jan 29 '13 at 15:19
  • I'll accept the answer as soon as SO allows me to accept it, which is tomorrow. – Nero Jan 29 '13 at 16:43
  • BTW, though you found a way to avoid the problem, I wouldn't actually say the problem is solved, per se. You found a workaround, but not the root cause of the problem. Since you don't have source for the point of the crash, finding the real root cause may be difficult or impossible without the source code. – phonetagger Jan 29 '13 at 18:35
  • Did you ever figure out how to get it running in XCode? I'm having the same issue and would prefer to build and run everything in the native environment. – Christopher Bales Mar 22 '13 at 16:23
  • Yes. Maybe I didn't make it clear enough: I recompiled the MySQL Connector/C++ with the -stdlib=libc++ flag. Now, when I compile the Xcode project with the new libraries, the program works fine. I'll add that to the answer. – Nero Mar 22 '13 at 17:27
  • How did you recompile the MySQL cpp connector? Did you have to download the Generic Linux Source? – Christopher Bales Mar 22 '13 at 18:48
  • As far as I know there is no precompiled binary for OS X. I downloaded the tar from here: http://dev.mysql.com/downloads/connector/cpp/ If you have more questions, it would probably be better to open a chat. – Nero Mar 22 '13 at 22:45
  • The instructions to compile are: cmake /path/to/source/mysql-connector-c++-1.1.3; make; make install; When do you add -stdlib=libc++ though? – joels May 29 '13 at 22:24
  • @joels I added it to the cmake command, so `cmake -DMYSQL_CXXFLAGS=... ...` and it worked for my version (1.1.1). If that does not work for 1.1.3, maybe this can help you: http://stackoverflow.com/q/8938805/1564895 – Nero May 30 '13 at 17:23
  • Okay so I am having serious issues with this crash... I created a post to this question here http://stackoverflow.com/questions/17980387/connection-crash-in-mysql-c. After reading your answer, I am still LOST on what to do... – jsetting32 Jul 31 '13 at 21:42
-1

I solved this problem by:

  1. download mysql-connector-c++-8.0.19-winx64
  2. link the libs, both mysqlcppconn8 and mysqlcppconn.lib make sure the connector is the right version. 8.0.19 works for me
tomkaith13
  • 1,717
  • 4
  • 27
  • 39