Reputation: 2848
This code works if it's in main:
int main(void)
{
cout << endl;
cout << "Running statement." << endl;
try {
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
/* Create a connection */
driver = get_driver_instance();
con = driver->connect("address:port", "user", "pass");
/* Connect to the MySQL test database */
con->setSchema("database");
stmt = con->createStatement();
stmt->execute("TRUNCATE TABLE words");
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;
}
That does exactly what it's supposed to. If I use the same code in a class method it will compile just fine, but the program crashes with:
"Unhandled exception at 0x0100DF5B in comClient.exe: Stack cookie instrumentation code detected a stack-based buffer overrun."
This happens at a line from "gs_report.c"
__fastfail(FAST_FAIL_STACK_COOKIE_CHECK_FAILURE);
The last line in my program(when this occurs) is the end of the method(at "}")
Why is this happening and what can I do to fix it?
[Edit]
I don't think this has much importance, but here's the class as well:
dataCom.h
#pragma once
/* Standard C++ includes */
#include <stdlib.h>
#include <iostream>
/* MySQL C++ Connector includes */
#include "mysql_connection.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
using namespace std;
class dataCom
{
public:
void clearDatabase();
};
As you can see I reduced it to one method containing the same code:
dataCom.cpp
#include "dataCom.h"
void dataCom::clearDatabase()
{
try
{
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
/* Create a connection */
driver = get_driver_instance();
con = driver->connect("address:port", "user", "pass");
/* Connect to the MySQL test database */
con->setSchema("database");
/* Create a statement */
stmt = con->createStatement();
stmt->execute("TRUNCATE TABLE words");
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;
}
}//<- crashes here
[Edit]
What other code? This is it. In main I just create an object and call the method. That's it.
However... I have been ignoring some warnings I don't fully understand:
c:\program files\mysql\mysql connector c++ 1.1.2\include\cppconn\sqlstring.h(38): warning C4251: 'sql::SQLString::realStr' : class 'std::basic_string<_Elem,_Traits,_Alloc>' needs to have dll-interface to be used by clients of class 'sql::SQLString'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Alloc=std::allocator<char>
1> ]
1>C:\Program Files\MySQL\MySQL Connector C++ 1.1.2\include\mysql_connection.h(165): warning C4251: 'sql::mysql::MySQL_Connection::proxy' : class 'boost::shared_ptr<T>' needs to have dll-interface to be used by clients of class 'sql::mysql::MySQL_Connection'
1> with
1> [
1> T=sql::mysql::NativeAPI::NativeConnectionWrapper
1> ]
1>C:\Program Files\MySQL\MySQL Connector C++ 1.1.2\include\mysql_connection.h(169): warning C4251: 'sql::mysql::MySQL_Connection::service' : class 'boost::scoped_ptr<T>' needs to have dll-interface to be used by clients of class 'sql::mysql::MySQL_Connection'
1> with
1> [
1> T=sql::mysql::MySQL_Statement
1> ]
1>C:\Program Files\MySQL\MySQL Connector C++ 1.1.2\include\cppconn/exception.h(61): warning C4251: 'sql::SQLException::sql_state' : class 'std::basic_string<_Elem,_Traits,_Alloc>' needs to have dll-interface to be used by clients of class 'sql::SQLException'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Alloc=std::allocator<char>
1> ]
Perhaps this is where the problem lies? How would I fix these warnings?
I tried making a function in main.cpp with the same code. Same problem. So I guess if I want to use the mysql connector in a function I have to make some modifications. But what and why?
Upvotes: 0
Views: 2166
Reputation: 2848
The solution to the "Stack cookie" problem was to build the connector driver(mysql c++ connector) and libmysql(mysql c connector) from source specifically for my version of visual studio.
If anyone else faces these problems, remember to build for the solution configuration you use in your application(debug for debug, release for release).
Upvotes: 4