std :: vector <std :: string>崩溃</std :: string>

时间:2011-01-29 10:29:49

标签: c++ debugging stl g++

这个问题是my question的继续。

这是有问题的代码。

A.H:

#include <string>
#include <vector>

std::vector<std::string> foo();

a.cpp

#include "a.h"

std::vector<std::string> foo()
{
   std::vector<std::string> v;
   return v;
}

最后是main.cpp:

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

int main()
{
    std::vector<std::string> s = foo();

    return 0;
}

编译如下(main.cpp使用STL调试标志编译):

g++ -c a.cpp
g++ -D_GLIBCXX_DEBUG main.cpp a.o

运行a.out时,进程崩溃:

Core was generated by `./a.out'.
Program terminated with signal 11, Segmentation fault.
#0  0x00007fe355998c43 in __gnu_debug::_Safe_iterator_base::_M_detach_single() () from /usr/lib64/libstdc++.so.6
(gdb) bt
#0  0x00007fe355998c43 in __gnu_debug::_Safe_iterator_base::_M_detach_single() () from /usr/lib64/libstdc++.so.6
#1  0x00007fe355999ebc in __gnu_debug::_Safe_sequence_base::_M_detach_all() () from /usr/lib64/libstdc++.so.6
#2  0x0000000000400cac in __gnu_debug::_Safe_sequence_base::~_Safe_sequence_base() ()
#3  0x0000000000400cc6 in __gnu_debug::_Safe_sequence<std::__debug::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >::~_Safe_sequence() ()
#4  0x0000000000400ce7 in std::__debug::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >::~vector() ()
#5  0x0000000000400c35 in main ()

我的gcc:

Using built-in specs.
Target: x86_64-suse-linux
Configured with: ../configure --prefix=/usr --infodir=/usr/share/info --mandir=/usr/share/man --libdir=/usr/lib64 --libexecdir=/usr/lib64 --enable-languages=c,c++,objc,fortran,obj-c++,java,ada --enable-checking=release --with-gxx-include-dir=/usr/include/c++/4.4 --enable-ssp --disable-libssp --with-bugurl=http://bugs.opensuse.org/ --with-pkgversion='SUSE Linux' --disable-libgcj --disable-libmudflap --with-slibdir=/lib64 --with-system-zlib --enable-__cxa_atexit --enable-libstdcxx-allocator=new --disable-libstdcxx-pch --enable-version-specific-runtime-libs --program-suffix=-4.4 --enable-linux-futex --without-system-libunwind --with-arch-32=i586 --with-tune=generic --build=x86_64-suse-linux
Thread model: posix
gcc version 4.4.1 [gcc-4_4-branch revision 150839] (SUSE Linux)

2 个答案:

答案 0 :(得分:12)

您的问题是将-D_GLIBCXX_DEBUG传递给a.cpp。此标志向STL结构添加其他调试信息,因此其使用必须在项目中的所有文件中保持一致。否则,不同的文件不同意std::vectorstd::string的内存布局,导致未定义的行为(在您的情况下崩溃)。

答案 1 :(得分:1)

在上一个问题中,您可以在此处参考GCC文档:http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt03ch17s04.html。该文档声明GCC libstdc ++“支持按用户重新编译”,并将其定义如下:

  

按使用重新编译:用户必须重新编译他或她的应用程序的部分以及它依赖于调试应该发生的位置的C ++库,以及与这些容器交互的任何其他代码。这意味着访问特定标准容器实例的一组转换单元可以在发布模式(无检查)或调试模式(完全检查)中编译,但必须以相同的方式编译;没有看到标准容器实例的翻译单元不需要重新编译。这也意味着包含在发布模式下编译的特定实例化(例如,std :: vector)的转换单元A可以链接到包含在调试模式下编译的相同实例化的转换单元B(部分重新编译时不存在的特性) )。虽然这种行为在技术上违反了单一定义规则,但这种能力在实践中往往非常重要。 libstdc ++调试模式支持这种级别的重新编译。

在每个单元的编译中,你正在尝试做这件事,它说:

  

我们认为,如果我们打算提供安全的迭代器,保持程序语义不变,并且在发布模式下不会降低性能,那么这种重新编译水平实际上是不可能的。

因此,我对你上一个问题的回答并不完全准确,我道歉。我已经添加了一个附录来纠正它,我希望这是一个有用的建议,如何解决那里的多库问题。