线程在检查数据库时抛出错误
Thread throws error while checking a database
下面是我为一个函数写的一些代码,该函数应该总是在自己的线程上运行,检查数据库中的新信息并将新信息附加到全局向量。我似乎遇到的问题是,经过这么多次,都是成功的,我将自发地得到这个错误:
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对象
- 分段错误当我试图运行程序时出错
- .h 和.cpp文件分离时出错,但仅使用 .h 文件时没有错误.我做错了什么?
- 创建 OpenCV 非自由版本 v4.3 时出错,可折叠.cpp错误 C2039、2605
- 制作时出错,C++"db.c:2127:错误:在"||"标记之前应使用";"
- C++语法错误,编译器不会警告或 int v = func(&v) 出错;
- 在VS2015中访问类成员时运行时错误,但在Linux上未访问时出错
- 如何更改路径以修复错误"./main:加载共享库 libmkl_core.so 时出错?
- 为什么在定义类之前声明类的对象会在友元类中给出错误,而在友元函数中不会出错
- 安装 mply 库时出错.致命错误:gsl/gsl_sf.h:没有这样的文件或目录
- Rcpp/C++/R:比较日期时间矢量与日期时间时出错(错误:"运算符>"的不明确重载)
- 编译特征程序时出错:错误:'seq'不是'Eigen'的成员
- 可能的 GCC 链接器错误会导致将弱符号和局部符号链接在一起时出错
- 非标准语法;使用 '&' 创建指向成员的指针错误,将成员函数分配给向量时出错
- 运算符重载时出错(错误:"运算符<<不匹配(操作数类型为"std::basic_ostream<char>"和"const char [2]")
- 编译类 C++ 中的线程时出错(错误 xthread)
- 加载GLEW库时出错(错误LNK2001)
- 通过函数指针调用函数时出错(错误C2064)
- 编译core.obj时出错:错误LNK2001:未解析的外部符号_harmony_core_init
- 链接c++静态库与android ndk时出错(错误:无法识别文件格式)
- 使用GCC编译游戏时出错.(错误:将"Screen"的含义从"class Screen&q