【问题标题】:g++ produces segfault with normal compilation, but none with -gg++ 在正常编译时会产生段错误,但在 -g 时不会产生段错误
【发布时间】:2011-10-06 04:03:51
【问题描述】:

我现在正在使用 Bruce Eckel 的“Thinking in C++”学习 C++,并且处于早期章节。我有 C 和 Java 背景。现在我遇到了以下问题:当我用

编译下面的源代码时
g++ A.cpp B.cpp bmain.cpp

,程序输出一个“1”(正确),然后是一个段错误。当我编译时

g++ -g A.cpp B.cpp bmain.cpp

,完全相同的程序产生 1 并且没有段错误!我不得不说我觉得这很惊人。有人可以指出我做错了什么吗?我的操作系统是“Linux 2.6.35-30-generic #54-Ubuntu x86_64”,我的 g++ 版本是“g++ (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5”。

编辑:仅仅因为这似乎是错误的一个重要来源,感谢@Evan Teran:B 结构中的 A 构造函数永远不会被调用!我放了一个 "cout

编辑:我现在在 main 末尾包含了“return 0”,但这无济于事。

啊哈:

#ifndef A_H
#define A_H

#include <string>

class A {
public:
        int i;
        std::string str;
        void print();
        A();
};

#endif

A.cpp:

#include "A.h"
#include <iostream>
#include <string>

using namespace std;

void A::print() {
        cout << str << " " << i << endl;
}

A::A() {
        str = "initstr";
    i = 0;
}

B.h:

#ifndef B_H
#define B_H

#include "A.h"

class B {
private:
        int counter;
public:
        A a;
        B();
        void increase();
        int read();
};

#endif

B.cpp:

#include "B.h"

using namespace std;

B::B() {
        counter = 0;
}

void B::increase() {
        ++counter;
}

int B::read() {
        return counter;
}

bmain.cpp:

#include <iostream>
#include "B.h"

using namespace std;

int main(int argc, char **argv) {
        B b;
        b.increase();
        cout << b.read() << endl;
        return 0;
}

编辑:我已经从包中安装了 g++。我的 Ubuntu 也很标准。

这是我打电话时得到的 gdb a.out 核心

warning: Can't read pathname for load map: Eingabe-/Ausgabefehler.
Reading symbols from /usr/lib/libstdc++.so.6...(no debugging symbols found)...done.
Loaded symbols for /usr/lib/libstdc++.so.6
Reading symbols from /lib/libm.so.6...Reading symbols from /usr/lib/debug/lib/libm-2.12.1.so...done.
done.
Loaded symbols for /lib/libm.so.6
Reading symbols from /lib/libgcc_s.so.1...(no debugging symbols found)...done.
Loaded symbols for /lib/libgcc_s.so.1
Reading symbols from /lib/libc.so.6...Reading symbols from /usr/lib/debug/lib/libc-2.12.1.so...done.
done.
Loaded symbols for /lib/libc.so.6
Reading symbols from /lib64/ld-linux-x86-64.so.2...Reading symbols from /usr/lib/debug/lib/ld-2.12.1.so...done.
done.
Loaded symbols for /lib64/ld-linux-x86-64.so.2
Core was generated by `./a.out'.
Program terminated with signal 11, Segmentation fault.
#0  0x00007fba1049104b in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() () from /usr/lib/libstdc++.so.6

编辑 2: 顺便说一句,据我所知,我的硬件没有故障,我对操作系统很好

编辑 3: Valgrind 报告如下:

==3428== Conditional jump or move depends on uninitialised value(s)
==3428==    at 0x4ECB022: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() (in /usr/lib/libstdc++.so.6.0.14)
==3428==    by 0x400D73: A::~A() (in /home/xxx/C++/Exercises/ch04/a.out)
==3428==    by 0x400D91: B::~B() (in /home/xxx/C++/Exercises/ch04/a.out)
==3428==    by 0x400CD7: main (in /home/xxx/C++/Exercises/ch04/a.out)
==3428== 
==3428== Use of uninitialised value of size 8
==3428==    at 0x4ECB04B: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() (in /usr/lib/libstdc++.so.6.0.14)
==3428==    by 0x400D73: A::~A() (in /home/xxx/C++/Exercises/ch04/a.out)
==3428==    by 0x400D91: B::~B() (in /home/xxx/C++/Exercises/ch04/a.out)
==3428==    by 0x400CD7: main (in /home/xxx/C++/Exercises/ch04/a.out)
==3428== 
==3428== Invalid read of size 4
==3428==    at 0x4ECB04B: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() (in /usr/lib/libstdc++.so.6.0.14)
==3428==    by 0x400D73: A::~A() (in /home/xxx/C++/Exercises/ch04/a.out)
==3428==    by 0x400D91: B::~B() (in /home/xxx/C++/Exercises/ch04/a.out)
==3428==    by 0x400CD7: main (in /home/xxx/C++/Exercises/ch04/a.out)
==3428==  Address 0xfffffffffffffff8 is not stack'd, malloc'd or (recently) free'd
==3428== 
==3428== 
==3428== Process terminating with default action of signal 11 (SIGSEGV): dumping core
==3428==  Access not within mapped region at address 0xFFFFFFFFFFFFFFF8
==3428==    at 0x4ECB04B: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() (in /usr/lib/libstdc++.so.6.0.14)
==3428==    by 0x400D73: A::~A() (in /home/xxx/C++/Exercises/ch04/a.out)
==3428==    by 0x400D91: B::~B() (in /home/xxx/C++/Exercises/ch04/a.out)
==3428==    by 0x400CD7: main (in /home/xxx/C++/Exercises/ch04/a.out)
==3428==  If you believe this happened as a result of a stack
==3428==  overflow in your program's main thread (unlikely but
==3428==  possible), you can try to increase the size of the
==3428==  main thread stack using the --main-stacksize= flag.
==3428==  The main thread stack size used in this run was 8388608.
==3428== 
==3428== HEAP SUMMARY:
==3428==     in use at exit: 0 bytes in 0 blocks
==3428==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==3428== 
==3428== All heap blocks were freed -- no leaks are possible    

【问题讨论】:

  • 适用于我在 Ubuntu 10.04.2 上使用和不使用 -gvalgrind 也没有报告任何问题。您确定在这两种情况下都运行了正确的可执行文件 (a.out) 吗?你有什么可能干扰你的LD_LIBRARY_PATH 环境变量吗?
  • 我看不出你的程序有什么问题。我怀疑编译器和/或环境损坏。
  • 这里也没有问题(Linux 2.6.38-10-generic #46-Ubuntu SMP Tue Jun 28 15:07:17 UTC 2011 x86_64 x86_64 x86_64 GNU/Linux)。您可以使用 gdb 或核心转储获取堆栈跟踪吗?
  • 笑一笑,试试“return 0;”在主要的末尾。你不应该需要它,但我很想看看这是否能解决你的问题。我真的认为与程序本身无关的东西导致了您的问题。
  • 你能做到gdb a.out,然后输入run,当它出现段错误时,输入bt(获取回溯)并在此处发布输出?

标签: c++ g++ segmentation-fault


【解决方案1】:

您几乎肯定会在程序中的某处调用未定义的行为 (UB)。 UB 的重点是行为不仅是未定义的,而且可能会根据平台、编译器、标志等而改变。添加 -g 可能会以这样的方式扰乱事物以避免段错误,但这只是机会.

【讨论】:

  • 未定义的行为在哪里?字段在两个类的构造函数中初始化。
  • @Andre:我还没有阅读代码;我只是为 OP 正在观察的行为提供最可能的机制。
  • 在这个特定程序中出现 UB 的可能性为零。
  • @Oli:那不是 UB。请参阅 C++0x FDIS 中的 3.6.1/5。虽然我完全同意这很可能是由 UB 引起的,尽管我在这里没有看到任何...
  • @Oli Charlesworth:可能是一个损坏的实现或损坏的环境。我不知道,我也没有。但我确实有这个程序在我眼前。任何解释都比UB好,因为这个程序没有任何UB。
【解决方案2】:

目前的情况似乎是 GCC/libstdc++ 打包/构建或使用的版本中的错误。试试 GCC 4.5 或 4.6,如果那里没有发生,告诉自己始终使用最新最好的(当然,直到它破坏某些东西)并且永远不要回头。

编译器似乎没有初始化B 中的A 成员,这将导致std::string 的析构函数无法读取必要的信息以正确自毁。但这只是推测和猜测。

【讨论】:

  • OMGWTFBBQ 我永远不会相信,但你是对的
  • 我现在已经从包中安装了 g++-4.5 并且它不再出现段错误 - 但它仍然令人讨厌和令人惊讶的是 4.4.5 应该表现出这样的行为 - 我的意思是如果这个婴儿程序没有'不编译,确实编译了什么
  • Hinton:所有软件都有bug,编译器和标准库是该领域最差的。他们往往会带出最糟糕的代码。请注意,这可能是由构建错误引起的。可以肯定的是,您应该重建当前的 GCC 4.4 并进行测试,但我并不是要您这样做(恕我直言,这没什么意义)。
  • @rubenvb:实际上我建议完全重建..但 OP 的代码,而不是 GCC。有时标头往往会被抛在后面。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-28
  • 1970-01-01
相关资源
最近更新 更多