问题描述
我有一个在 qtcreator 中运行良好的 Qt5 应用程序,但是如果我尝试通过终端创建的可执行文件运行我得到
I have a Qt5 application that runs fine in the qtcreator, but if I try to run in by the executable created through terminal I get
Segmentation fault (core dumped)
我在 Qt 的调试模式下尝试过,但没有错误.
I've tried in debug mode in Qt but no errors.
推荐答案
如果程序在调试器外运行时崩溃,但在调试器内运行时没有崩溃,这可能表明您正在使用未初始化的数据.更具体地说,一个未初始化的指针.
If a program crashes when run outside of a debugger, but doesn't crash when run inside the debugger, it might be a sign that you are using uninitialized data. More specifically, an uninitialized pointer.
调试器通常会清除所有数据,包括局部变量.这意味着例如在调试器中运行时,指针将为 NULL
.但是如果你不初始化一些局部变量,它的内容在调试器之外运行时将是不确定的,你对 NULL
的检查会说这不是 NULL,请继续",你会引用这个未初始化的指针并进入未定义行为的领域.
Debuggers generally clears all data, including local variables. That means that e.g. a pointer will be NULL
when run in the debugger. But if you don't initialize some local variable, its contents will be indeterminate when run outside of the debugger, and your check against NULL
will say "this is not NULL, please continue", and you will reference this uninitialized pointer and enter the territory of undefined behavior.
您需要遍历所有局部变量,尤其是指针,并确保在使用它们之前对其进行初始化.
You need to go through all local variables, especially pointers, and make sure you initialize them before using them.
这篇关于Qt5 应用程序中的分段错误(核心转储)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!