我在 C 语言中使用 sqlite3 和 ubuntu 14.04。 在我的过程中,我每 20 分钟更新一次数据库。
这是我的功能:
int update_alive_state(uint64_t ieee,bool isAlive)
{
char *error_report = NULL;
char sql[100];
int result = -1;
UpdateAliveStateSql(sql,ieee,isAlive);
if(result = sqlite3_exec(db_event, sql, 0, 0, &error_report))
{
printf( "\t> CMD: %s , Error: %s\n" , sql , error_report );
sqlite3_free(error_report);
}
return result;
}
当 sqlite3_exec()
返回错误并打印错误消息时,我收到段错误消息并且进程终止。
ERROR: signal 11 was trigerred:
Fault address: 0xae703f1b
Fault reason: address not mapped to object
Stack trace folows:
./main.bin(segmentation_fault_handler+0xd5)[0x419e9f]
/lib/x86_64-linux-gnu/libpthread.so.0(+0x10330)[0x7fa386f20330]
/lib/x86_64-linux-gnu/libc.so.6(_IO_vfprintf+0x1d03)[0x7fa386257943]
/lib/x86_64-linux-gnu/libc.so.6(_IO_printf+0x99)[0x7fa3862603d9]
./main.bin(update_alive_state+0x15d)[0x43ca78]
./main.bin(EventScanThread+0x2af)[0x442c21]
/lib/x86_64-linux-gnu/libpthread.so.0(+0x8184)[0x7fa386f18184]
/lib/x86_64-linux-gnu/libc.so.6(clone+0x6d)[0x7fa38630637d]
Executing original handler...
如果我注释掉 printf
和 sqlite3_free
,段错误消息就会消失。
为什么我不能打印错误信息?
请帮助我。
编辑: 我这样更改了我的代码
if(result = sqlite3_exec(db, sql, 0, 0, &error_report))
{
printf( "[ERR] : \t> CMD: %s , Error: %d\n" , sql , result );
if ( error_report )
{
printf( "[ERR] : Error msg: %s\n", error_report );
sqlite3_free(error_report);
}
}
但我仍然收到段错误消息。
ERROR: signal 11 was trigerred:
Fault address: 0x7f3dc490cf1b
Fault reason: address not mapped to object
Stack trace folows (partial):
./main.bin(segmentation_fault_handler+0xd5)[0x419e9f]
/lib/x86_64-linux-gnu/libpthread.so.0(+0x10330)[0x7fab749ac330]
/lib/x86_64-linux-gnu/libc.so.6(_IO_vfprintf+0x1d03)[0x7fab73ce3943]
/lib/x86_64-linux-gnu/libc.so.6(_IO_printf+0x99)[0x7fab73cec3d9]
./main.bin(update_last_receive_time+0x166)[0x43d60e]
./main.bin(attr_process_attribute_report_ind+0x97)[0x4222dc]
./main.bin(si_gateway_incoming_data_handler+0xa3)[0x41b6b1]
./main.bin(tcp_socket_event_handler+0x558)[0x41c316]
./main.bin(polling_process_activity+0xde)[0x41a4e1]
./main.bin(main+0x150)[0x41a0f4]
Executing original handler...
错误地址指向第一个printf函数
printf( "[ERR] : \t> CMD: %s , Error: %d\n" , sql , result );
为什么?请帮我。谢谢。
最佳答案
If the 5th parameter to sqlite3_exec() is not NULL and no errors occur, then sqlite3_exec() sets the pointer in its 5th parameter to NULL before returning.
所以有可能根本没有错误消息,你应该这样做:
if (result = sqlite3_exec(db_event, sql, NULL, NULL, &error_report))
{
if (error_report)
{
printf( "\t> CMD: %s , Error: %s\n" , sql , error_report );
sqlite3_free(error_report);
}
}
检查它是否设置为 NULL
。
I changed my code like this [...]
But I still get segmentation fault message. [...]
The error address points to the first printf function:printf( "[ERR] : \t> CMD: %s , Error: %d\n" , sql , result );
如果使用 %s
打印字符数组,则 printf 需要一个空终止的 '\0'
C-string .由于您没有提供 UpdateAliveStateSql
函数,因此我看不到 sql
字符串是否以 null 结尾。为确保通过在缓冲区末尾写入 '\0'
,您可以:
sql[sizeof(sql) - 1] = '\0';
调用 UpdateAliveStateSql
函数后。
关于c - 打印 sqlite3_exec 错误消息导致 C 中的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44880865/