Error connecting MySQL / C ++ BAD ACCESS

Using C ++ in Xcode I'm trying to access a MySQL database using the MySQL Connector / C ++. The problem is that a program (compiled with Xcode) always crashes with

EXC_BAD_ACCESS (code=13, address=0x0) 

upon call

 driver->connect(url, user, pass) 

In Xcode, I created a complete new project (OS X> Command Line Tool), pasted the code (see below) into main.cpp, the added Boost and MySQL Connector header includes paths as well as libmysqlcppconn.6.1.1.1. dylib as a link library and click on the Run button.

Next, when I compile the program manually using

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

the program works fine, and also runs the INSERT statement in the table.

The program code is taken from the MySQL Connector / C ++ examples, namely the pthreads.cpp example, but truncated to the main 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; } 
+4
source share
1 answer

Ok, the problem is resolved.

Here the problem was in one compiler. The MySQL / C ++ connector was compiled without using -stdlib=libc++ , but Xcode added this compilation / link flag to its commands. It caused a crash. This also explains why the compiled program manually worked, since I did not include this flag in the compilation command.

To make this clearer: I recompiled the MySQL Connector / C ++ with the -stdlib=libc++ flag. Then the program compiled by Xcode is great for me. To compile MySQL Connector / C ++, I added

 -DMYSQL_CXXFLAGS=-stdlib=libc++ 

to the cmake command, which should be executed when the connector is installed.

 make VERBOSE=1 

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

+7
source

All Articles