C++ MySQL 连接器 PrepareStatement Bad_Access

标签 c++ raspberry-pi mysql-connector

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

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

CallStack

关于成员(member)值(value)的说明;

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 错误。

所以在分配内存时要小心。

关于C++ MySQL 连接器 PrepareStatement Bad_Access,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40701003/

相关文章:

c# - 是否有适用于 Visual Studio 2012 的 mySQL 连接器?

c++ - Lua:避免 pcall 和 Lua 调用堆栈重载

c++ - std::vector 随机添加元素到 vector

linux - 如何处理来自摄像机的实时流的 V4L2 中的各个帧?

linux - CGI 脚本不执行 bash 命令,例如 'CP'

java - 将 isvalid() 函数与 mysql 连接对象一起使用时出现 AbstractMethodError

c++ - 无法在我的 OSX 中编译 "hello world"cpp

c++ - 如何计算我的程序运行需要多少毫秒?

java - tomcat7 一直说它正在运行,应用程序不工作(Raspberry Pi)

java - SQL语法中的MySQLSyntaxErrorException