4

I'm having a bit of issues with trying to run a compiled program that uses the MySQL connector, in C++. It compiles just fine, but when running it, it'll crash immediately - seemingly so on the line that's meant to connect. I've set up all additional libraries, dependencies, pre-processors and linker inputs, and I'm using the Release solution configuration. I am running Microsoft Visual Studio 2012.

The error I'm getting is the following: Unhandled exception at 0x6E69AF48 (msvcr90.dll) in MyLittleSQL.exe: 0xC0000005: Access violation reading location 0x00000024.

And the call stack:

MyLittleSQL.exe!main() Line 24 C++
MyLittleSQL.exe!__tmainCRTStartup() Line 536 C

Line 24 is:

con = driver->connect("tcp://127.0.0.1:3306", "sepples_su", "easy");

And the full source code is:

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

#include "mysql_connection.h"
#include "mysql_driver.h"

#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
using namespace std;

int main(void)
{
    try 
    {
        sql::Driver *driver;
        sql::Connection *con;
        sql::Statement *stmt;
        sql::ResultSet *res;

        /* Create a connection */
        driver = get_driver_instance();
        con = driver->connect("tcp://127.0.0.1:3306", "sepples_su", "easy");

        /* Connect to the MySQL test database */
        con->setSchema("test");

        stmt = con->createStatement();
        res = stmt->executeQuery("SELECT 'Hello World!' AS _message");
        while (res->next()) 
        {
            cout << "\t... MySQL replies: ";
            /* Access column data by alias or column name */
            cout << res->getString("_message") << endl;
            cout << "\t... MySQL says it again: ";
            /* Access column fata by numeric offset, 1 is the first column */
            cout << res->getString(1) << endl;
        }

        delete res;
        delete stmt;
        delete con;
    } 
    catch (sql::SQLException &e) 
    {
        cout << "# ERR: SQLException in " << __FILE__;
        cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
        cout << "# ERR: " << e.what();
        cout << " (MySQL error code: " << e.getErrorCode();
        cout << ", SQLState: " << e.getSQLState() << " )" << endl;
    }

    cout << endl;
    return EXIT_SUCCESS;
}

This is actually one of the examples taking from the Connector documentation, but I wanted to make sure it wasn't my own fault first.

Any help here would be appreciated, thank you.

mtaanquist
  • 506
  • 4
  • 16
  • 2
    Is the connector's DLL compiled with Visual Studio 2012? Using libraries, compiled with different compilers causes crashes like this. – Lyubomir Vasilev Nov 29 '12 at 07:47
  • Is there a way to check this? The DLL was shipped compiled through the archive at mysql.com. – mtaanquist Nov 29 '12 at 07:55
  • Are you sure you are using the latest version? – Mark Garcia Nov 29 '12 at 08:04
  • @MadsT See this for determining which compiler the dll was build with: http://stackoverflow.com/questions/764329/determining-which-compiler-built-a-win32-pe – Lyubomir Vasilev Nov 29 '12 at 08:06
  • @MarkGarcia Yes, I've downloaded the latest Connector from MySQL, and I'm keeping VS2012 up-to-date. – mtaanquist Nov 29 '12 at 08:11
  • @LyubomirVasilev Having had a look with PEiD, it's showing "Linker Info=9.0", which I guess is why it's crashing in msvcr90.dll, too. I know that's what VS2008 used, but would that be incompatible with 2010/2012? – mtaanquist Nov 29 '12 at 08:14
  • @MadsT I think yes. I think your program should be built with vs2008 to work. Or, the dll can be built with vs2012 (is there source, I don't know). Could you try building your program with vs2008, if it is available? – Lyubomir Vasilev Nov 29 '12 at 08:27
  • @LyubomirVasilev Thank you so much! I compiled the connector from scratch using VS11, and I've got it working now. Much appreciated! – mtaanquist Nov 29 '12 at 09:16
  • @Lyubomir Vasileve, I have thesame problem and getString is crashing. I have tried everything but the only solution left is to compile the connector my self. I am using Visual Studio 13 and don't know how to do this. The linker Info of my dll is 10.0. Can you show me to compile this? The link Mads Taanquist provided is moved and no longer exist. – Programmer May 28 '15 at 07:39
  • 1
    @Programmer Here is the new link http://dev.mysql.com/doc/connector-cpp/en/connector-cpp-installation-source-windows.html . However, I haven't tested but I think that even with the newest version of CMake, you can use up to `Visual Studio 12 2013`. I don't see Visual Studio 12 in the supported generators here: http://www.cmake.org/cmake/help/v3.0/manual/cmake-generators.7.html . – Lyubomir Vasilev May 28 '15 at 11:04
  • Thanks for the link. – Programmer May 28 '15 at 15:11

2 Answers2

3

With help from @LyubomirVasilev I compiled the C++ Connector on my own with settings for Visual Studio 11 (2012). Replacing the other lib and dll files with the compiled here, it worked perfectly after.

Information on doing this can be found here for any others: http://dev.mysql.com/doc/refman/5.1/en/connector-cpp-info.html

cp.engr
  • 2,291
  • 4
  • 28
  • 42
mtaanquist
  • 506
  • 4
  • 16
2

The latest C++ MySQL connector is compiled with VC9 runtime libraries (Visual studio 2008). Visual studio 2012 uses VC11 libraries so it's obvious why your program crashed. Your program must use the same runtime libraries as MySQL C++ connector:

Unhandled exception at 0x6E69AF48 (msvcr90.dll) <--- VC9

You must compile your program with Visual studio 2008 which uses VC9 libraries or compile MySQL C++ connector from source with Visual studio 2012.

BJovke
  • 1,737
  • 15
  • 18