while循环中的MySQL Connector / C ++错误

时间:2017-01-02 03:10:35

标签: c++ mysql mysql-connector

void dataProcess::loginProcess(byte* packet, uint16_t length)
{
    unique_ptr<ResultSet> res;
    unique_ptr<Statement> stmt;
    byte idPacket[4] = {packet[9], packet[8], packet[7], packet[6]};
    uint32_t id, account_id;
    string account_pass, account_type;
    id = decodeToInt(idPacket, 4);
    account_pass = decodeToChar(packet, 14, length - 1);
    account_type = decodeToChar(packet, 10, 11);
    stmt.reset(this->con->createStatement());
    res.reset(stmt->executeQuery("SELECT * FROM account_info"));
    while(res->next())
    {
        account_id = res->getUInt("id");
        if(lowerCase(account_type).compare("ts") == 0)
        {
            if((account_id == id) && (account_pass.compare(res->getString("password")) == 0))
            {
                sendPacket("F4440300010300");
            }
            else
            {
                wrongPass();
            }
        }
        else
        {
            wrongPass();
        }
    }
}

所以在无限while循环的每次迭代中调用上述函数一次,并且函数在第二次迭代时总是失败。我认为这在某种程度上与第一次迭代时删除ResultSet对象有关,因为当我尝试使用普通指针并使用delete语句时会出现同样的问题,但是如果我删除了删除它可以通过第二次迭代,但我'我确定需要根据MySQL网站上的示例删除ResultSet。我是mysql Connector / C ++的新手,所以我不确定导致这个问题的原因。从调试开始,在第二次迭代时,这一行似乎失败了。

res.reset(stmt->executeQuery("SELECT * FROM account_info"));

这是程序进入第二次迭代后出现的错误

Error in `./server': free(): invalid size: 0x00007f8214004ca0 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x790cb)[0x7f8219aa70cb]
/lib/x86_64-linux-gnu/libc.so.6(+0x8275a)[0x7f8219ab075a]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7f8219ab418c]
/usr/lib/x86_64-linux-gnu/libmysqlcppconn.so.7(_ZN5boost20checked_array_deleteIc                          EEvPT_+0x1f)[0x7f821a9397df]
/usr/lib/x86_64-linux-gnu/libmysqlcppconn.so.7(_ZN5boost12scoped_arrayIcED1Ev+0x                          1b)[0x7f821a9390b5]
/usr/lib/x86_64-linux-gnu/libmysqlcppconn.so.7(_ZN3sql5mysql15MySQL_ResultSetC2E                          N5boost10shared_ptrINS0_9NativeAPI22NativeResultsetWrapperEEENS2_8weak_ptrINS4_2                          3NativeConnectionWrapperEEENS_9ResultSet9enum_typeEPNS0_15MySQL_StatementERNS3_I                          NS0_17MySQL_DebugLoggerEEE+0x205)[0x7f821a98cde3]
/usr/lib/x86_64-linux-gnu/libmysqlcppconn.so.7(_ZN3sql5mysql15MySQL_Statement12e                          xecuteQueryERKNS_9SQLStringE+0x11b)[0x7f821a99443f]
./server(+0x326e)[0x56192448126e]
./server(+0x2f40)[0x561924480f40]
./server(+0x2da3)[0x561924480da3]
./server(+0x2a45)[0x561924480a45]
/lib/x86_64-linux-gnu/libpthread.so.0(+0x76ca)[0x7f8219dfc6ca]
/lib/x86_64-linux-gnu/libc.so.6(clone+0x5f)[0x7f8219b360af]

类和构造函数:

class dataProcess : public thread
{
    public:
        dataProcess(int sock);
        virtual ~dataProcess();

    protected:
    virtual void thread_handler();
    private:
    void wrongPass();
    void loginProcess(byte* packet, uint16_t length);
    void sendPacket(string packet);
    int sock;
    player* p;
    Driver* driver;
    unique_ptr<Connection> con;
};
dataProcess::dataProcess(int sock)
{
    this->sock = sock;
    driver = get_driver_instance();
    con.reset(driver->connect("localhost", "root", "password"));
    con->setSchema("ts_server");
    thread::startThread();
}

1 个答案:

答案 0 :(得分:0)

好的家伙我已经解决了这个问题,我作为一个新手学到了非常重要的一课。出现问题是由于我粗心造成的未定义行为,以确保我已经删除了所有动态分配的变量&#34;一旦我在正确的位置添加了删除,一切都很完美。所以面对这些随机问题的人一定要检查这些:)