このコードは、メインにある場合に機能します。
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);
私のプログラムの最後の行(これが発生したとき)は、メソッドの終わりです(「}」で)
なぜこれが起こっているのですか?それを修正するにはどうすればよいですか?
[編集]
これはあまり重要ではないと思いますが、クラスも次のとおりです。
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();
};
ご覧のとおり、同じコードを含む 1 つのメソッドに減らしました。
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
[編集]
他のコードは?これです。メインでは、オブジェクトを作成してメソッドを呼び出すだけです。それでおしまい。
しかし...私は完全に理解していないいくつかの警告を無視してきました:
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> ]
問題はここにあるのではないでしょうか?これらの警告を修正するにはどうすればよいですか?
同じコードで main.cpp に関数を作ってみました。同じ問題。したがって、関数で mysql コネクタを使用する場合は、いくつかの変更を加える必要があると思います。しかし、何となぜ?