std :: vector< std :: string>崩溃 [英] std::vector<std::string> crash

查看:171
本文介绍了std :: vector< std :: string>崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题是我的问题的延续。

这里是有问题的代码。

啊:

#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:

and finally main.cpp:

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

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

    return 0;
}

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

Compilation as following (main.cpp is compiled with STL debugging flag):

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

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

When running a.out, process crashes:

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)


推荐答案

在您以前的问题中,您可以参考GCC文档: http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt03ch17s04.html 。该文档指出,GCC libstdc ++支持每用户重新编译,并定义如下:

In your previous question, you reference the GCC documentation here: http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt03ch17s04.html. That documentation states that the GCC libstdc++ "supports per-user recompilation", and defines it as follows:


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

Per-use recompilation: The user must recompile the parts of his or her application and the C++ libraries it depends on where debugging should occur, and any other code that interacts with those containers. This means that a set of translation units that accesses a particular standard container instance may either be compiled in release mode (no checking) or debug mode (full checking), but must all be compiled in the same way; a translation unit that does not see that standard container instance need not be recompiled. This also means that a translation unit A that contains a particular instantiation (say, std::vector) compiled in release mode can be linked against a translation unit B that contains the same instantiation compiled in debug mode (a feature not present with partial recompilation). While this behavior is technically a violation of the One Definition Rule, this ability tends to be very important in practice. The libstdc++ debug mode supports this level of recompilation.

在单位编译中,这是您尝试在这里做的,它说:

Of per-unit compilation, which is what you're trying to do here, it says:


我们认为,如果我们打算提供安全的迭代器,那么这个重新编译水平实际上是不可能的,让程序语义保持不变,而不是在发布模式下的性能退化....

We believe that this level of recompilation is in fact not possible if we intend to supply safe iterators, leave the program semantics unchanged, and not regress in performance under release mode....

因此,我对您以前的问题的回答并不完全准确,我道歉。我已经添加了一个补充来纠正它,希望对于如何解决多库问题有用的建议。

Thus, my answer on your previous question was not entirely accurate, and I apologize. I've added an addendum to it to correct it, with what I hope is a useful suggestion about how to solve the multi-library problem there.

这篇关于std :: vector&lt; std :: string&gt;崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆