gpt4 book ai didi

c++ - 线程在检查数据库时抛出错误

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

下面是我为一个函数编写的一些代码,该函数应该始终在自己的线程上运行,检查数据库中的新信息并将新信息附加到全局 vector 。我似乎遇到的问题是,经过这么多次,一切都成功了,我会自发地得到这个错误:

Project2.exe 中 0x5B46F1F9 (libmysql.dll) 处的未处理异常:0xC0000005:访问冲突读取位置 0x000003B0。

不太确定发生了什么,但它似乎是在 if(connect){} 语句之后发生的,因为就在它崩溃时我看到了 Connection Failed! 的打印语句>。我只是不知道为什么会失败。我遇到了一个类似的问题,该问题是由达到数据库的最大连接数引起的,但我不知道这是怎么发生的,因为在每次迭代 if 语句后,我都使用 mysql_close(connect ) 语句。

void getNewFromDB(){
while(globalExit == false){
MYSQL *connect; // Create a pointer to the MySQL instance
connect=mysql_init(NULL); // Initialise the instance
/* This If is irrelevant and you don't need to show it. I kept it in for Fault Testing.*/
if(!connect) /* If instance didn't initialize say so and exit with fault.*/
{
fprintf(stderr,"MySQL Initialization Failed");


}
/* Now we will actually connect to the specific database.*/

connect=mysql_real_connect(connect,SERVER,USER,PASSWORD,DATABASE,0,NULL,0);
/* Following if statements are unneeded too, but it's worth it to show on your
first app, so that if your database is empty or the query didn't return anything it
will at least let you know that the connection to the mysql server was established. */

if(connect){
printf("Connection Succeeded\n");
}
else{
printf("Connection Failed!\n");
}


MYSQL_RES *result; /* Create a pointer to recieve the return value.*/
MYSQL_ROW row; /* Assign variable for rows. */

mysql_query(connect,"SELECT * FROM locationTime ORDER BY id DESC");
/* Send a query to the database. */

result = mysql_store_result(connect); /* Receive the result and store it in res_set */

unsigned int numrows = mysql_num_rows(result); /* Create the count to print all rows */

row = mysql_fetch_row(result);
if(row[0] > ids.back()){
ids.push_back(row[0]);
dateTime.push_back(row[1]);
locs.push_back(setLocation(row[2]));
imgPaths.push_back(row[3]);
}
mysql_close(connect);
}
/* Close and shutdown */

}

编辑:所有给出的答案都解决了部分问题,谢谢。但是现在我不明白的是为什么在 16000 个请求之后会突然连接失败?

最佳答案

 MYSQL *connect; // Create a pointer to the MySQL instance
connect=mysql_init(NULL); // Initialise the instance
/* This If is irrelevant and you don't need to show it. I kept it in for Fault Testing.*/
if(!connect) /* If instance didn't initialize say so and exit with fault.*/
{
fprintf(stderr,"MySQL Initialization Failed");
}

在那部分代码之后,出现有关失败的消息,您的“connect”未初始化,因此您收到访问冲突,因为您将“connect”投入运行。无需初始化,它可以指向虚拟内存的任何部分。这就是你遇到问题的原因。

这样修复:

 MYSQL *connect = NULL; // Create a pointer to the MySQL instance
connect=mysql_init(NULL); // Initialise the instance
/* This If is irrelevant and you don't need to show it. I kept it in for Fault Testing.*/
if(!connect) /* If instance didn't initialize say so and exit with fault.*/
{
fprintf(stderr,"MySQL Initialization Failed");
return;
}

您还应该使用库的标准方式来打印错误编号和错误原因,以了解发生了什么。使用:

fprintf(stderr, "%s\n", mysql_error(connect));

UPD:你也可能有内存泄漏:

connect=mysql_real_connect(connect,SERVER,USER,PASSWORD,DATABASE,0,NULL,0);

因此,您永远不会释放从

收到的连接对象
connect=mysql_init(NULL);

行。您只需通过指向由 mysql_real_connect 返回的连接的指针来编写指向它的指针。

这就是为什么在 16k 成功之后,你可能会因为缺少内存分配而失败。 It告诉返回指针指向第一个参数的拷贝。

关于c++ - 线程在检查数据库时抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24517691/

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