【问题标题】:Segmentation fault at run time in c++ class [closed]c++ 类中运行时的分段错误[关闭]
【发布时间】:2013-05-24 10:48:03
【问题描述】:

您好,我在运行以下代码时看到此错误。 “分段错误(核心转储)”我无法解释原因。\有人帮助我。谢谢

#ifndef __LIB_TAG_STRINGS_H__
#define __LIB_TAG_STRINGS_H__

//============
//stl
#include <deps.h>
#include <string>
namespace tags
 {
   const std::string tag1 ("TAG_1");
   const std::string tag2 ("TAG_2");
 }//END namespace TAGS

namespace attributes
 {
  const std::string attribute1 ("ATTRIBUTE_1");
  const std::string attribute2("ATTRIBUTE_2");
 }

class _Name 
 {
  public:

  _Name ()
   {
    /**This constructor is used to map some tags with the strings**/
    string_map.insert (std::make_pair (std::string ("TAGNOTE1"), tags::tag1 ));
    string_map.insert (std::make_pair (std::string ("TAGNOTE2"), tags::tag2 ));
   };

 const std::string& getName (const std::string& class_name) const
   {   
    std::map<std::string, std::string>::const_iterator i = string_map.find(class_name);
    return (i != string_map.end ()) ? i->second : null_string;
  }


   private:
    std::map<std::string, std::string> string_map;
    std::string null_string;
 };

namespace Name
 {
  const _Name NAME;
 }
#endif

核心在运行时被转储。这是 GDB 指向错误的代码。

GDB 日志:

程序收到信号 SIGSEGV,分段错误。 0x0000003b00e9d23b in std::basic_string, std::allocator >::basic_string(std::basic_string, std::allocator > const&) () 来自 /usr/lib64/libstdc++.so.6 缺少单独的调试信息,请使用:debuginfo-install glibc-2.12-1.80.el6.x86_64 libaio-0.3.107-10.el6.x86_64 libgcc-4.4.6-4.el6.x86_64 libstdc++-4.4.6-4.el6。 x86_64 nss-softokn-freebl-3.12.9-11.el6.x86_64 openssl-1.0.0-20.el6_2.5.x86_64 zlib-1.2.3-27.el6.x86_64 (gdb) 在哪里 0 0x000strong text0003b00e9d23b in std::basic_string, std::allocator >::basic_string(std::basic_string, std::allocator > const&) () 来自 /usr/lib64/libstdc++.so.6 1 0x00007ffff1375372 在 _Name::_Name (this=0x7ffff15a9b60) 在 /prog/lib/tag_strings.h:229 2 0x00007ffff139581b 在 __static_initialization_and_destruction_0 () 在 /prog/lib/tag_strings.h:257 3 个全局构造函数在 dest.cxx:679 处键入 dest.cxx(void) () 4 ./dest.so 的 __do_global_ctors_aux () 中的 0x00007ffff1398516 5 0x00007ffff135a2bb in _init () from ./dest.so 6 0x00007fffe361f000 在?? () 7 0x0000003af860e535 in _dl_init_internal () from /lib64/ld-linux-x86-64.so.2 8 0x0000003af8600b3a in _dl_start_user () from /lib64/ld-linux-x86-64.so.2 9 0x0000000000000001 在?? () 10 0x00007ffffffffe128 在?? ()。 11 0x0000000000000000 在 ?? ()。

【问题讨论】:

  • 嗨,我在运行以下代码时看到了这个错误。 “分段错误(核心转储)”我无法解释原因。\有人帮助我。谢谢
  • main() 在哪里? &lt;deps.h&gt; 是什么?
  • 好的,GDB 会告诉你崩溃在哪里。你介意告诉我们崩溃在哪里吗?
  • @AndyProwl deps.h 包含包含的头文件列表
  • 实际上 std::basic_string 中的崩溃与我的回答中的解释完全一致:-)

标签: c++ stl


【解决方案1】:

在我看来像一个静态初始化顺序问题。

当您的 NAME 对象被构造时,它会访问 tag1/tag2 对象 - 但不能保证这些对象已经被构造了!

您可以尝试更改依赖于函数的对象。

namespace tags {
    static std::string tag1() { return std::string("TAG1"); }
    ...
}

...
string_map.insert (std::make_pair (std::string ("TAGNOTE1"), tags::tag1() ));
...

【讨论】:

  • 非常感谢@hmn 代码现在正在运行。我想问一下为什么以前的代码在 solaris 上运行良好。
  • tag1、tag2 和 NAME 的初始化顺序是未定义的——你可以认为它是随机的。有时重新编译代码以使这种崩溃(暂时)消失就足够了。
猜你喜欢
  • 2021-07-18
  • 2016-07-31
  • 1970-01-01
  • 1970-01-01
  • 2019-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多