c++ mysql connection bad_alloc using c++ connector

两盒软妹~` 提交于 2019-12-12 19:34:16

问题


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


回答1:


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




回答2:


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=".



来源:https://stackoverflow.com/questions/39101671/c-mysql-connection-bad-alloc-using-c-connector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!