C++ MySQL 连接器 PrepareStatement Bad_Access

C++ MySQL Connector PrepareStatement Bad_Access

在运行时循环两次,在通过请求查询调用 PrepareStatement 时因 Bad_Access 崩溃。

所以我检查了所有的ResultSet内存并释放了它但是没有想法 有解决这个问题的办法吗?

关于会员价值的说明;

PrepareStatement* pstmt;

ResultSet* res;

和完整代码

bool laskdjlaskdj12::RaspBerry::grup_pw_chk(const Json::Value j){

//check the id and group is valid
try{

    string group = j["Group"].asString();
    const char* id = j["id"].asCString();
    string grp;
    string pub;
    BIO* tmp;                           //BIO structor to change the RSA* structor

    if(group == "NULL"){
        group = "";
    }
    //request the Client is existence
    pstmt = con->prepareStatement("SELECT * FROM `raspberry_cli` WHERE `Cli_id` = (?) AND `Cli_Group` = (?)");
    pstmt->setString(1, id);
    pstmt->setString(2, group);

    res = pstmt->executeQuery();

    //if query reply is NULL
    if(res->next() == false){
        std::cout<<"[INFO] : There is no query about raspberry_cli"<<std::endl;
        return false;
    }
    //if query Reply 
    grp = res->getString("Cli_Group");
    pub = res->getString("Cli_Pub");
    //if There is no group in Raspberry_pi
    if(ras.grp.compare(grp) == false){
        //sql의 버퍼 flush
        this->SQL_Flush();
        return false;
    }
    // if the group is equal
    else{
        //save the client information
        acc_cli.S_id = id;
        acc_cli.S_Group = grp;

        //save the client public_key
        tmp = BIO_new_mem_buf(pub.c_str(), -1);
        acc_cli.Pub_key = PEM_read_bio_RSA_PUBKEY(tmp, NULL, 0, NULL);
        //if public key is not wright in section
        if(acc_cli.Pub_key == NULL){
            return false;
        }
        delete[] tmp;
        //flush SQL_flush  = delete ResultSet
        this->SQL_Flush();
        return true;
    }
}catch(sql::SQLException& e){
    std::cout << "# ERR: SQLException in " << __FILE__;
    std::cout << "(" << __FUNCTION__ << ") on line "<< __LINE__ << std::endl;
    std::cout << "# ERR: " << e.what();
    std::cout << " (MySQL error code: " << e.getErrorCode();
    std::cout << ", SQLState: " << e.getSQLState() << " )" << std::endl;
    return false;
}

Detal StackTrace 是这个

* thread #1: tid = 0x2778b, 0x00000001000e29c3 RaspBerry`list_add(root=0x0000000100903e38, element=0x00000001009043e8) + 19 at list.c:33, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
frame #0: 0x00000001000e29c3 RaspBerry`list_add(root=0x0000000100903e38, element=0x00000001009043e8) + 19 at list.c:33 [opt]
frame #1: 0x00000001000de27f RaspBerry`mysql_stmt_init(mysql=0x0000000102802220) + 143 at libmysql.c:1568 [opt]
frame #2: 0x0000000100168f41 RaspBerry`sql::mysql::NativeAPI::MySQL_NativeConnectionWrapper::stmt_init(this=0x0000000100a00580) + 33 at mysql_native_connection_wrapper.cpp:421 [opt]
frame #3: 0x000000010011c415 RaspBerry`sql::mysql::MySQL_Connection::prepareStatement(this=0x0000000100a00550, sql=0x00007fff5fbfe138) + 53 at mysql_connection.cpp:1137 [opt]
* frame #4: 0x0000000100073071 RaspBerry`laskdjlaskdj12::RaspBerry::grup_pw_chk(this=0x00007fff5fbfeba0, j=const Json::Value @ 0x00007fff5fbfea30) + 881 at RaspBerry.cpp:438
frame #5: 0x000000010007c222 RaspBerry`main(argc=1, argv=0x00007fff5fbff7f8) + 3474 at main.cpp:81
frame #6: 0x00007fffced8b255 libdyld.dylib`start + 1
frame #7: 0x00007fffced8b255 libdyld.dylib`start + 1

找到答案

pstmt = con->prepareStatement("SELECT * FROM `raspberry_cli` WHERE `Cli_id` = (?) AND `Cli_Group` = (?)");
pstmt->setString(1, id);
pstmt->setString(2, group);

res = pstmt->executeQuery();

[delete pstmt;]      //===================> This part is missing

在这一部分我没有删除 pstmt(sql::preparestatement*) 所以 sql 发送了那个 malloc 错误。

所以要小心分配内存。