2016-11-20 52 views
0

在通過兩次循環運行時,在同時通過請求查詢調用PrepareStatement墜毀的Bad_AccessC++ MySQL連接PrepareStatement Bad_Access

所以,我檢查了所有的ResultSet內存和釋放這一切,但沒有想法 任何想法解決這個問題?

CallStack

說明有關部件值;

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堆棧跟蹤是這樣的

* 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 
+0

錯誤提示「Raspberry_Pi已編譯優化 - 步進可能表現異常;變量可能不可用」。 –

+0

將帖子作爲文字發送至 – Danh

+0

@marc_s Thankyou –

回答

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 

在這一部分,我沒有刪除數PreparedStatement pstmt( sql :: preparestatement *) 所以sql已經發送了malloc錯誤。

所以請注意分配內存。