简体   繁体   中英

Runtime error with debug build of project using MySQL Connector/C++

I am using the MySQL Connector/C++ in my project for interaction with the MySQL database. The project compiles fine. However, when I run it I get an error

Unhandled exception at at 0x000007FEFD30B16D in ProjectName.exe: 
Microsoft C++ exception: std::bad_alloc at memory location 0x00000000036FC2A0.

on the first line using the database. In this case I am executing a MySQL statement

sql::Statement* stmt = mSqlCon->con->createStatement();
stmt->execute("CREATE TABLE IF NOT EXISTS ..."); // Unhandled exception

When I compile for Release, everything works fine and no errors are thrown.

My environment:

  • Windows 7, 64bit
  • Visual Studio Express 2012

Visual Studio project settings and libraries that might be relevant:

  • Preprocessor definitions: _DEBUG;_MBCS;_CRT_SECURE_NO_WARNINGS
  • Runtime library: Multi-threaded Debug (/MTd)
  • Basic Runtime Checks: Both (/RTC1, equiv. to /RTCsu) (/RTC1)
  • MySQL Connector/C++ 1.1.6 (Release version)
  • Boost Version 1.59.0

My thoughts: This error occurred after I started using the boost library. This required to change some project settings, like preprocessor definition _DEBUG (which should be used for debug builds anyway, but was not used before to be able to build the connector). So I think the problem is using the release version of MySQL Connector/C++ in the debug build of the project. Everything works fine in the release build. However, I cannot find any debug version of the connector, nor can I build it myself or reinstall it with debug libraries... Is there another solution to my problem (or did I not search good enough)?

I read that there should be a debug folder in my MySQL folder C:\\Program Files\\MySQL\\MySQL Connector C++ 1.1.6\\lib , but it only contains an opt folder with the release build (which I am using).

Any help is very much appreciated!

Yes you are right, the problem is behind linking a project in Debug configuration with the Release library inside the opt folder. All you have to do is compile yourself the source code of the library in Debug configuration:

  1. From the MySQL website , click on Select Platform, click on Source Code and download the windows version of the library
  2. Download and install CMake if you don't have it
  3. Open a command prompt and change directory to the directory of the source code you downloaded
  4. Type (path\\to\\)cmake.exe -G "Visual Studio 14 2015 Win64" -DCMAKE_BUILD_TYPE=Debug and run it; this way you are telling CMake to build a project that you can compile with the version 14 of Visual Studio (you can choose other versions, if you want to have a look type cmake -h ) for a x64 Machine with Debug configuration.
  5. At this point you should see some Visual Studio Projects in the library source folder- open the file MYSQLCONNCPP.sln . Change the output file type to static if you wish to create a static library.
  6. Go to Solution Properties and only select the MYSQLCONNCPP project to build. a) Now go to Project Properties, and add HAVE_STRUCT_TIMESPEC to the Preprocessor Definitions (this prevents timespec redefinition) b) Add /EHsc to C++ -> Command Line (this prevents errors related to the exception handler) c) Change the project's Runtime Library to match the one you want to link the library to: for instance in my case I had to set it as /MDd
  7. In case of other errors, check if all the Project Properties that CMake has built are well written: it might have had issues with some paths and file names with spaces inside.

I got the same problem. It happens because of discrepency in the configuration. The client application can be debugged if the mysql wrapper is a separate .lib file. If the Mysql release version has been downloaded and installed then you need to build a wrapper to connect/query etc and export it to other projects as a release .lib. The Mysql can be debugged using trace option provided. MYSQL DEBUG TRACE C++

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM