2

Trying to build a simple mysql connection, but getting a bad_alloc and I can't figure out how to solve this, even looking at similar posts

here is my code

#include <iostream>
#include <stdlib.h>
#include <sstream>
#include <memory>
#include <string>
#include <stdexcept>

#include "mysql_connection.h"
#include "cppconn\driver.h"
#include "cppconn\statement.h"
#include "cppconn\connection.h"
#include "cppconn\exception.h"
#include "cppconn\prepared_statement.h"
#include "cppconn\statement.h"


using namespace std;
using namespace sql;

int main()
{

    Driver *driver;
    Connection *conn;
    Statement *stmt;

    driver = get_driver_instance();
    conn = driver->connect("127.0.0.1:3306", "root", "root"); // breaks here
    stmt = conn->createStatement();
    stmt->execute("USE mydb");

    return 0;
}

I'm using mysql-connector-c++-1.1.7 mysqlcppconn.lib is used as a dependencies mysqlcppconn.dll is located in the same dir as the .exe is.

Here is the error Exception thrown at 0x000007FEFD39A06D in MysqlConn.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x000000000014F5D0.

Thanks

abdoe
  • 359
  • 1
  • 4
  • 11
  • *mysqlcppconn.lib is used as a dependencies and it is located in the same dir as the .exe is* -- A .lib file is used in the build process and has nothing to do with where the `exe` is located. It seems you aren't aware of what components are used to build your application and which components are involved in running your app. Maybe the MySql DLLs that you're supposed to be using at runtime are not compatible with your executable? – PaulMcKenzie Aug 23 '16 at 12:55
  • 1
    sorry, I meant .dll, I'll fix this. I followed this introduction and used dymanic instead of statis build https://dev.mysql.com/doc/connector-cpp/en/connector-cpp-apps-windows-visual-studio.html according to this I don't need any mysql dlls anymore I guess? :( – abdoe Aug 23 '16 at 12:56
  • 1
    Well, you need to make sure that the DLL's (it seems they use the C++ standard library), are compatible with your compiler's version of the standard library. In other words,if those DLL's were created using VS 2013, and you're using another version of Visual Studio, then there will be runtime issues. If it's a static library, the same thing applies. The static library must be built with the same compiler, compiler options, etc. as your application. – PaulMcKenzie Aug 23 '16 at 12:58
  • I used this connector mysql-connector-c++-1.1.7-winx64.msi with library machine type x64. Could you kindly let me know what I can do to check what you suggested? – abdoe Aug 23 '16 at 13:01
  • hmm okay, yes I'm using vs2015 But I wonder whiy i can find no information about a dll for visual studio 2015, I cant be the only one having this troubles. Maybe I'm the only one not aware of this , so ;) – abdoe Aug 23 '16 at 13:05
  • I tried to recompile it from source using cmake and the compile it with vs15. :X but no luck so far . – abdoe Aug 23 '16 at 14:35

2 Answers2

1

I also had this error. In my case I am compiling using VS2015 in Windows.

First time I choose compile static version of the MySQL lib. Then later I decided to compile the dynamic version. This time the error bad_alloc at memory went off.

The solution is rolling back the CPPCONN_PUBLIC_FUNC= configuration.

Go to project Property Pages, under C++ > Preprocessor > Preprocessor Definitions and remove the item CPPCONN_PUBLIC_FUNC=".

AStopher
  • 4,207
  • 11
  • 50
  • 75
Renato Aloi
  • 300
  • 2
  • 9
0

I had the same error on linux.

The error was : I was using g++-4.8 to build the project

The issue lies on the version of the build tools (gcc,msvs,clang) used to build the project

Victor Mwenda
  • 1,677
  • 17
  • 16