gpt4 book ai didi

C++ MySQL 连接器 PrepareStatement Bad_Access

转载 作者:行者123 更新时间:2023-11-28 05:25:11 31 4
gpt4 key购买 nike

在运行时循环两次,在通过请求查询调用 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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com