如果它在 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;
}
这正是它应该做的。如果我在类方法中使用相同的代码,它会编译得很好,但程序会崩溃:
“comClient.exe 中 0x0100DF5B 处未处理的异常:堆栈 cookie 检测代码检测到基于堆栈的缓冲区溢出。”
这发生在“gs_report.c”的一行
__fastfail(FAST_FAIL_STACK_COOKIE_CHECK_FAILURE);
我程序的最后一行(发生这种情况时)是方法的结尾(在“}”处)
为什么会发生这种情况,我能做些什么来解决它?
[编辑]
我不认为这很重要,但这里也是类:
数据通信
#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();
};
如您所见,我将其简化为一种包含相同代码的方法:
数据通信.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
[编辑]
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?