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

Thread throws error while checking a database

本文关键字:出错 错误 数据库 检查 线程      更新时间:2023-10-16

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

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 Succeededn");
    }
    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 */
}

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

 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, "%sn", mysql_error(connect));

UPD:也可能有内存泄漏:

connect=mysql_real_connect(connect,SERVER,USER,PASSWORD,DATABASE,0,NULL,0);
所以,你永远不会释放从 接收到的连接对象
connect=mysql_init(NULL);

。你只需通过mysql_real_connect返回的指向连接的指针重写指向它的指针。

这就是为什么在16k成功之后,您可能会因为缺乏内存来分配而失败的原因。它告诉返回指针指向第一个形参的副本

如果connect为false,则打印"Connection failed",但之后在查询调用中仍使用无效的connect对象

相关文章: