天天看点

linux下调用mysql_query产生SIGSEGV段错误



程序需要用到MySQL来进行持久化,代码在windows测试通过,但在Linux调试时,产生下面的异常:

Thread 9 "NodesManagerd" received signal SIGSEGV, Segmentation fault.      

[Switching to Thread 0x7ffff2b08700 (LWP 9389)]                                                  

0x00007ffff77f1dc0 in ?? () from /usr/lib/x86_64-linux-gnu/libmysqlclient.so.20

通过调试,最后定位到是执行mysql_query产生的问题:

265   assert(_handle != nullptr && sql != nullptr);                                                    

(gdb) n                                                                                                                          

266   if (mysql_query(_handle, sql) != 0)                                                                   

(gdb) n                                                                                                                          

Thread 9 "NodesManagerd" received signal SIGSEGV, Segmentation fault.       

0x00007ffff77f1dc0 in ?? () from /usr/lib/x86_64-linux-gnu/libmysqlclient.so.20 

_handle不为空,sql(SELECT NodeId, LastKeepAlive, Status FROM NodeStatus;)正常。起初不明白为什么出现这样的问题,sql没有问题,问题只能出现在_handle。查看_handle初始化方式如下:

_handle = mysql_init(&_mysql);

将初始化方式改为:

_handle = mysql_init(nullptr);

问题解决。但不知为什么第一种方式在windows上正常运行,在linux却当掉。