c++如何解决段错误 (核心已转储)

在后端优化中遇到了段错误 (核心已转储)问题,寻求解决方法。
在这里插入图片描述首先通过不断的注释部分代码,运行,注释,运行,找到一个大概的范围是错在哪一块。

这类问题只能说根本原因是内存的非法操作。建议生成内存段错误转储文件,发生段错误后用gdb分析该文件定位到发生段错误的代码上,这样才能确定导致段错误的直接原因。 -国庆

GDB大法好,直接定位到相关的信息上去,使用gdb ggoLearning 进入调试界面

gdb ggoLearning 
GNU gdb (Ubuntu 8.1-0ubuntu3.2) 8.1.0.20180409-git
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ggoLearning...done.

进入(gdb)调试界面,输入r,可以运行程序,可以看到程序在运行的时候,出现了Program received signal SIGSEGV, Segmentation fault.错误。

当程序出现异常时通常伴随着会收到一个由内核发过来的异常信号,如当对内存出现非法访问时将收到段错误信号SIGSEGV,然后才退出。利用这一点,当我们在收到异常信号后将程序的调用栈进行输出,它通常是利用signal()函数,关于系统信号的

(gdb) r
Starting program: /home/user/work/x/rgbd-slam-tutorial-gx/partI/bin/ggoLearning 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
RMSE for the structure is:                0.00825072
RMSE for the rotation of camera is:       0.0478788
RMSE for the translation of camera is:    0.0220988

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff75e6267 in g2o::BaseVertex<6, g2o::SE3Quat>::pop() ()
   from /home/user/work/x/rgbd-slam-tutorial-gx/partI/include/g2o/lib/libg2o.so

bt可以看到程序的好几层,使用backtrace回溯定位问题。这里有一个例子:在Linux中如何利用backtrace信息解决程序崩溃的问题

(gdb) bt
#0  0x00007ffff75e6267 in g2o::BaseVertex<6, g2o::SE3Quat>::pop() ()
   from /home/user/work/x/rgbd-slam-tutorial-gx/partI/include/g2o/lib/libg2o.so
#1  0x00007ffff7620a78 in g2o::SparseOptimizer::pop(std::vector >&) ()
   from /home/user/work//rgbd-slam-tutorial-gx/partI/include/g2o/lib/libg2o.so
#2  0x00007ffff7629be2 in g2o::OptimizationAlgorithmLevenberg::solve(int, bool)
    ()
   from /home/user/work/x/rgbd-slam-tutorial-gx/partI/include/g2o/lib/libg2o.so
#3  0x00007ffff76219cd in g2o::SparseOptimizer::optimize(int, bool) ()
   from /home/user/work/xio/rgbd-slam-tutorial-gx/partI/include/g2o/lib/libg2o.so
#4  0x000055555581f5a0 in SolveBA (
    vNoisyCameras=std::vector of length 3, capacity 3 = {...}, 
    vpNoiseGeometryFeatures=std::vector of length 2, capacity 2 = {...})
    at /home/user/work/x/rgbd-slam-tutorial-gx/partI/src/ggoLearning.cpp:477
#5  0x000055555581c4bd in main (argc=1, argv=0x7fffffffdd18)
    at /home/user/work/x/rgbd-slam-tutorial-gx/partI/src/ggoLearning.cpp:123
(gdb) frame 5
#5  0x000055555581c4bd in main (argc=1, argv=0x7fffffffdd18)
    at /home/user/work/x/rgbd-slam-tutorial-gx/partI/src/ggoLearning.cpp:123
warning: Source file is more recent than executable.
123	    SolveBA(vNoisyCameras,vNoisyGeomeryPlaneFeatures);

我们一般使用各类的库

(gdb) frame 4
#4  0x000055555581f5a0 in SolveBA (
    vNoisyCameras=std::vector of length 3, capacity 3 = {...}, 
    vpNoiseGeometryFeatures=std::vector of length 2, capacity 2 = {...})
    at /home/user/work/xinguo/rgbd-slam-tutorial-gx/partI/src/ggoLearning.cpp:477
477	    optimizer.optimize(10);
(gdb) 

输入quit或者按下Ctrl-d退出

调试到后面发现进入了汇编语言界面,。。。

HLT 使 CPU 进入这么一个状态:既不取指令,也不读写数据,总线上“静悄悄”的。这条指令
用的地方不多,一般用于等外部中断。

你可能感兴趣的:(C++学习笔记,GDB,c++,debug)