Access reading violation when creating a database connection


I am trying to write a basic http server in c++, and i need to fetch information from a local database. I'm using MySql c++ connector with the legacy API but on connection creation i receive this exception:

Unhandled exception at 0x00007FFE69031416 (vcruntime140d.dll) in HTTP_SERVER.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.

with this code:

    void Database_connection::connect(sql::SQLString& host, sql::SQLString& username, 
        sql::SQLString& password, sql::SQLString& DBName) {

        try {
            sql::Driver* sqlDriver = get_driver_instance();
            // enstablish connection
            if (sqlDriver != nullptr) {
                connection = sqlDriver->connect(host, username, password);

            // use the given database
        } catch (sql::SQLException e) {
            std::cout << e.what() << " // " << e.getErrorCode() << " // " << e.getSQLState() << " // " << std::endl;
        } catch (std::exception& e) {
            std::cout << e.what() << std::endl;
        } catch (...) {
            std::cout << "throwed something" << std::endl;

specifically on connection = sqlDriver->connect(host, username, password);

Now, this only happen in release mode, in debug everything is fine; the strings host, username and password are correct, and the driver should also be fine.

Can you help me?

asked on Stack Overflow May 26, 2021 by Leonardo Montagner • edited May 26, 2021 by Leonardo Montagner

2 Answers


Most likely the driver throws an exception which is derived from sql::SQLException. As you catch by value, you cannot catch derived exceptions.

Instead you should accept by reference (this is a general rule: throw by value, catch by reference). That way you get access to the what() string which should tell you in more details what's actually going wrong.

Additionally, get_driver_instance might throw an exception, too, but call to resides outside of the try-catch block.

Finally, you use a different variable driver (member variable? – declaration is missing) to open the connection than the one you assigned the driver object to (the object assigned to local variable is leaking!).

answered on Stack Overflow May 26, 2021 by Aconcagua • edited May 26, 2021 by Aconcagua

Fixed it myself, still don't know what the problem is but i re-downloaded release and debug libraries from connector c++ and rebuild with the appropriate libraries

answered on Stack Overflow May 26, 2021 by Leonardo Montagner

User contributions licensed under CC BY-SA 3.0