下面是我为一个函数编写的一些代码,它应该始终在自己的线程上运行,检查数据库是否有新信息,并将新信息附加到全局向量。我似乎遇到的问题是,经过这么多次成功,我会自发地得到这个错误:
Unhandled exception at 0x5B46F1F9 (libmysql.dll) in Project2.exe: 0xC0000005: Access violation reading location 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之后一些请求会突然连接失败?
答案 0 :(得分:1)
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告诉返回指针指向第一个参数的副本。
答案 1 :(得分:0)
如果connect为false,则打印“Connection failed”,但之后在查询调用中仍使用无效的connect对象。