Q应用程序执行分段错误
QApplication Execution Segmentation Fault Error
任何人都能帮我吗。我正在尝试使用cmakefiles运行一个应用程序。在我的程序的主文件上,当程序到达执行QAppication的代码行时,我遇到了分段错误。下面是片段代码:
int main(int argc, char** argv)
{
bool viewing;
parse_command_line( argc, argv );
#ifdef _GRAPHICS_
glutInit(&argc, argv);
#endif
if( viewing )
{
#ifdef _GRAPHICS_
QApplication application(argc, argv);
Viewer *viewer = new Viewer( 0, exp, argc, argv );
Interface *render = new Interface( 0, exp, viewer );
render->show();
return application.exec(); //this line causes the segmentation fault
delete viewer;
delete render;
#endif
}
}
您的问题是,应用程序对象在其他使用它的对象之前就被销毁了。您应该利用C++语义来帮助您做到这一点,并在使用时摆脱令人讨厌的手动内存管理:
int main(int argc, char** argv)
{
bool viewing;
parse_command_line( argc, argv );
#ifdef _GRAPHICS_
glutInit(&argc, argv);
#endif
if (viewing) {
#ifdef _GRAPHICS_
QApplication app(argc, argv);
Viewer viewer( 0, exp, argc, argv );
Interface render( 0, exp, &viewer );
render.show();
return app.exec();
#endif
}
}
以上内容是必要的,因此您必须进行更改以避免未定义的行为。但是,如果您的Viewer
或Interface
实现存在错误,则修复可能还不够。
相关文章:
- 在某些循环内使用vector.push_back时出现分段错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 当我的阵列太大时出现分段错误
- 分段错误当我试图运行程序时出错
- 在c++中初始化矩阵时出现分段错误(核心转储)
- 尝试使用集合函数时出现分段错误
- 我无法缩小此分段错误的原因
- g++的分段错误(在NaN上使用to_string两次时)
- 为什么我使用 std::copy() 收到运行时错误(分段错误)?
- 为什么我的代码包含错误分段错误(核心转储)?
- 为什么会出现此错误?- 分段错误(核心转储)
- 意外的运行时错误(分段错误)
- C++将文本文件读入数组,运行错误:分段错误
- mmap 错误:分段错误/指针无效错误
- 在我的代码中找不到错误....分段错误
- 错误:分段故障(核心转储)阵列
- 用string.h将错误分段到队列中
- c++字符串流转换错误:分段错误
- 为什么我的代码显示错误分段错误(核心转储)