MySQL Connector/C++ error in while loop

236 Views Asked by At
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();
        }
    }
}

so the above function is being called once in each iteration of an infinite while loop and the function always failed at the 2nd iteration. I think this is somehow related to the deletion of the ResultSet object at the first iteration because when I try using a normal pointer and using the delete statement the same problem occurs but if I remove the delete it can get through the 2nd iteration but I'm quite sure that the ResultSet needs to be deleted according to the example on MySQL website. I am quite new to mysql Connector/C++ so I am very unsure to what is causing this problem. From debugging it appears to fail at this line on the second iteration.

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

This is the error that appears after the program enters the second iteration

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 and the constructor:

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

There are 1 best solutions below

0
On

Ok guys I have solved this problem and I have learned a very important lesson as a newbie. The problem occurs due to the undefined behaviour caused by my carelessness to ensure I have "deleted all my dynamically allocated variables" Once I have added delete in the right place everything works perfectly. so anyone facing these kind of random problems make sure to check these :)