为什么mysql c ++连接器在main()中工作但在类方法中不工作?

时间:2013-04-07 20:07:47

标签: c++ visual-studio-2010 mysql-connector

如果代码在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);

我程序中的最后一行(发生这种情况时)是方法的结尾(在“}”处)

为什么会发生这种情况,我该怎么做才能解决它?

[编辑]

我认为这不重要,但这里也是课程:

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();
};

如您所见,我将其缩减为包含相同代码的一种方法:

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

[编辑]

还有什么其他代码?就是这个。在main中我只是创建一个对象并调用该方法。就是这样。

但是......我一直忽略了一些我不完全理解的警告:

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连接器,我必须做一些修改。但是为什么以及为什么?

1 个答案:

答案 0 :(得分:4)

“堆栈cookie”问题的解决方案是专门为我的Visual Studio版本从源代码构建连接器驱动程序(mysql c ++连接器)和libmysql(mysql c连接器)。

如果其他人遇到这些问题,请记住构建您在应用程序中使用的解决方案配置(调试调试,发布版本)。