Levi Botelho
Levi Botelho

Reputation: 25234

MySQL Connector C++ - Invalid Pointer

I am trying to use the MySQL C++ Connector to connect to a database. I have added the libraries and the source code compiles correctly with all the necessary #include statements. The code I am using is the following:

#include <stdlib.h>
#include <iostream>

#include "mysql_connection.h"

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

using namespace std;

int main(void)
{
    using namespace sql;

    Driver *driver;
    Connection *con;

driver = get_driver_instance();
con = driver -> connect("tcp://127.0.0.1:3306/test", "test", "test");
}

The code is taken right from the examples and should work properly. I have shortened the code significantly, as it is at the final "driver -> connect" line where the error is thrown. My error message is the following:

*** glibc detected *** /home/username/NetBeansProjects/mysql/dist/Release/GNU-Linux-x86/mysql:
free(): invalid pointer: 0x091dd468 ***

I'm on Linux Mint Lisa, running the latest version of MySQL and using NetBeans 7.1 as an IDE. As mentioned, the code compiles correctly, and it is on this last connection line where the error occurs. Any help or recommendations for another connection mechanism would be much appreciated.


UPDATE

Here is the code from the Driver class where the connection is defined

class CPPCONN_PUBLIC_FUNC Driver
{
protected:
virtual ~Driver() {}
public:
// Attempts to make a database connection to the given URL.

virtual Connection * connect(const sql::SQLString& hostName, const sql::SQLString& userName, const sql::SQLString& password) = 0;

virtual Connection * connect(ConnectOptionsMap & options) = 0;

...

Nothing to see there... in my humble opinion...

Upvotes: 4

Views: 3717

Answers (2)

tony gil
tony gil

Reputation: 9574

Have your C++ programs talking to MySQL in under 2 minutes!

NOTE: this works on Ubuntu 12.04 servers (AWS EC2) with MySQL (typical LAMP setup), no backward compatibilty guaranteed.

To access MySQL databases from a C++ program, you must first install some mysql library files using the following command line

sudo apt-get install libmysqlclient-dev

save a configuration file called "mysql_config" in the same directory where you will save (and run your app - let us say "/temp" )

mysql_config --cflags
-I/usr/include/mysql  -DBIG_JOINS=1  -fno-strict-aliasing   -DUNIV_LINUX
mysql_config --libs
-Wl,-Bsymbolic-functions -rdynamic -L/usr/lib/mysql -lmysqlclient

compile your c++ file "myapp.cpp", with

sudo g++ -o myapp $(mysql_config --cflags) myapp.cpp $(mysql_config --libs)

based on this excellent functional example written by Coding Friends

Upvotes: 1

Keagan Ladds
Keagan Ladds

Reputation: 458

I have found the solution to this problem for Ubuntu 12.04, it has taken me about 48 hours of no sleep but ok, here goes. Im sure this fix will work for other versions of Ubuntu. Download the libstdc++.so.5 from http://www.sopcast.com/download/libstdcpp5.tgz. You need to extract and place the files in /usr/lib.

Make sure you add -llibstdc++.so.5 when building your app with g++ ( this links stdc++ v5)

Then Google 'Connector c++ Ubuntu 12.04' and look for the Builds and download the .deb for your Arch.

For Ubuntu 12.04 here is the URL for the .debs https://launchpad.net/ubuntu/precise/+source/mysql-connector-c++/+builds Choose your arch ie. Amd64 and download the two .debs under the Built Files. Open and install using the package manager. You should be able to run you app now without and issues.

EDIT You do not need to link libstdc++.so.5, you might have to on your system but on Ubuntu 12.04 amd64 its not needed.

Upvotes: 3

Related Questions