c++ - 奇怪的段错误

标签 c++ segmentation-fault gdb

我收到一个奇怪的段错误。我在下面粘贴了代码的相关部分。

当我在 gdb 中运行代码时,我得到以下输出:

DERPE
DERPH0 1

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_PROTECTION_FAILURE at address: 0x00007fff5f3ffff0
0x000000010000fcee in Interval::contains (this=0x100103c90, other={isValid = 93, s = 1, e = 7, n = 5}) at interval.cpp:33
33      if (!contains(other.s)) {

A.cpp

void A::foo(){
    ...
    std::cout << "DERPE" << std::endl;
    std::cout << "DERPH" << id << " " << curr << std::endl;
    \\graph is a vector<B>
    std::cout << "DERPI" << id << " " << curr << " " << graph[id].bar(graph[curr]) << std::endl;
    ...
}

B.cpp

bool B::bar(B &other) const {
    // each B contains 2 Intervals, adj, opp
    return (other.adj.contains(this->adj) and 
           (!this->opp.isValid or other.adj.contains(this->opp) or   
           (other.opp.isValid and other.opp.contains(this->opp))));
}

间隔.cpp

bool Interval::contains(Interval other) const {
    if (!contains(other.s)) {
    return false;
    }
    ...
}

当我尝试使用 gdb 时,我得到以下信息:

(gdb) p other
$1 = {
  isValid = 93,
  s = 1,
  e = 7,
  n = 5
}
(gdb) p this
$2 = (const Interval *) 0x100103c90
(gdb) p *this
$3 = {
  isValid = 93,
  s = 1,
  e = 0,
  n = 5
}
(gdb) p other.s
$4 = 1
(gdb) p (*this).contains(other.s)

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_PROTECTION_FAILURE at address: 0x00007fff5f3fff50
Interval::contains (this=Cannot access memory at address 0x7fff5f3fff50
) at interval.cpp:28
28  bool Interval::contains(int x) const {
The program being debugged was signaled while in a function called from GDB.
GDB remains in the frame where the signal was received.
To change this behavior use "set unwindonsignal on"
Evaluation of the expression containing the function (Interval::contains(int) const) will be abandoned.
(gdb) p this
Cannot access memory at address 0x7fff5f3fff50

关于如何处理这个问题有什么想法吗?我迷路了。

编辑: Interval的完整头文件如下:

区间.h

#ifndef INTERVAL_H
#define INTERVAL_H

#include <vector>

    struct Interval {
        bool isValid;
        int s, e, n;

        Interval() {};
        Interval(int n);
        Interval(int start, int end, int n);
        bool operator==(const Interval other) const;
        int length() const;
        bool contains(int x) const;
        bool contains(Interval other) const;
    };
#endif

我没有复制构造函数,但我认为一切都只是原语,所以默认的复制构造函数可以工作。

我又试了一下,这似乎很相关:

(gdb) backtrace 10
#0  0x000000010000fcce in Interval::contains (this=0x100103c90, other={isValid = 34, s = 1, e = 6, n = 5}) at interval.cpp:33
#1  0x000000010000fd9b in Interval::contains (this=0x100103c90, other={isValid = 34, s = 1, e = 6, n = 5}) at interval.cpp:52
#2  0x000000010000fd9b in Interval::contains (this=0x100103c90, other={isValid = 34, s = 1, e = 6, n = 5}) at interval.cpp:52
#3  0x000000010000fd9b in Interval::contains (this=0x100103c90, other={isValid = 34, s = 1, e = 6, n = 5}) at interval.cpp:52
#4  0x000000010000fd9b in Interval::contains (this=0x100103c90, other={isValid = 34, s = 1, e = 6, n = 5}) at interval.cpp:52
#5  0x000000010000fd9b in Interval::contains (this=0x100103c90, other={isValid = 34, s = 1, e = 6, n = 5}) at interval.cpp:52
#6  0x000000010000fd9b in Interval::contains (this=0x100103c90, other={isValid = 34, s = 1, e = 6, n = 5}) at interval.cpp:52
#7  0x000000010000fd9b in Interval::contains (this=0x100103c90, other={isValid = 34, s = 1, e = 6, n = 5}) at interval.cpp:52
#8  0x000000010000fd9b in Interval::contains (this=0x100103c90, other={isValid = 34, s = 1, e = 6, n = 5}) at interval.cpp:52
#9  0x000000010000fd9b in Interval::contains (this=0x100103c90, other={isValid = 34, s = 1, e = 6, n = 5}) at interval.cpp:52    
(gdb) backtrace
#0  0x000000010000fcce in Interval::contains (this=0x100103c90, other={isValid = 34, s = 1, e = 6, n = 5}) at interval.cpp:33
...
#67667 0x000000010000fd9b in Interval::contains (this=0x100103c90, other={isValid = 34, s = 1, e = 6, n = 5}) at interval.cpp:52
(gdb)

最佳答案

我想通了。无限回溯意味着 contains 中的递归处理不当(我缺少一个基本情况),因此它无限递归 -> 堆栈溢出 -> 段错误。

关于c++ - 奇怪的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30599781/

相关文章:

assembly - 使用 gdb 和 QEMU 调试引导加载程序/BIOS 时如何跳过中断调用?

c++ - Pantheios:太大时创建一个新的日志文件

c++ - 触摸板 + QToolButton = supperios press

C++ - Brent-Pollard rho 算法无限循环

linux - 虚幻引擎在 Linux 上启动时崩溃

c++ - 为什么我没有收到此代码的段错误? (总线错误)

c++ - XFixesCursorImage 的结构

c++ - 如何一次创建、处理和销毁多个窗口?

c - 反向 C 字符串 - 简单的错误

c - 无法从 .gdbinit 获取文件源