The initialization sequence of static map members is related to the compiler.

Source: Internet
Author: User

I can't believe this is true, but it did happen, and it took me 5 hours.

The core file content is like this:

#0 0x0000003071664cba in STD: _ rb_tree_decrement (STD: _ rb_tree_node_base *) () from/usr/lib64/libstdc ++. so.6
(GDB) BT
#0 0x0000003071664cba in STD: _ rb_tree_decrement (STD: _ rb_tree_node_base *) () from/usr/lib64/libstdc ++. so.6
#1 0x0000000000494520 in STD: _ rb_tree_iterator <STD: pair <STD: basic_string <char, STD: char_traits <char>, STD :: allocator <char> const, void * (*)>: Operator -- (this = 0x7fff252b0810) at/usr/lib/GCC/x86_64-redhat-linux/4.1.2 /.. /.. /.. /.. /include/C ++/4.1.2/bits/stl_tree.h: 197
#2 0x0000000000494801 in STD: _ rb_tree <STD: basic_string <char, STD: char_traits <char>, STD: Allocator <char>, STD :: pair <STD: basic_string <char, STD: char_traits <char>, STD: Allocator <char> const, void * (*)>, STD :: _ select1st <STD: pair <STD: basic_string <char,
STD: char_traits <char>, STD: Allocator <char> const, void * (*)>, STD: less <STD: basic_string <char, STD: char_traits <char>, STD: Allocator <char>, STD: Allocator <STD: pair <STD: basic_string <char, STD :: char_traits <char>, STD: Allocator <char> const,
Void * (*) () >>:: insert_unique (
This = 0x831e20, _ v = ...) at/usr/lib/GCC/x86_64-redhat-linux/4.1.2 /.. /.. /.. /.. /include/C ++/4.1.2/bits/stl_tree.h: 929
#3 0x00000000004948e3 in STD: Map <STD: basic_string <char, STD: char_traits <char>, STD: Allocator <char>, void *(*) (), STD: less <STD: basic_string <char, STD: char_traits <char>, STD: Allocator <char >>, STD: Allocator <STD:: pair <STD: basic_string <char, STD: char_traits <char>,
STD: Allocator <char> const, void * (*) () >>:: insert (this = 0x831e20, _ x = ...)
At/usr/lib/GCC/x86_64-redhat-linux/4.1.2/.../include/C ++/4.1.2/bits/stl_map.h: 396
#4 0x00000000004939d4 in tac: AGENT: agentfactory: registclass (name = "caderequestagent ",
Method = 0x493882 <register <TAC: AGENT: caderequestagent, (char const *) (char *) (& (TAC: AGENT: caderequestagentargv)>: createinstance ()>)
At src/tac_agent_factory.cpp: 49
#5 0x00000000004938cb in registyclass: registyclass (this = 0x831df8, name = "caderequestagent ",
Method = 0x493882 <register <TAC: AGENT: caderequestagent, (char const *) (char *) (& (TAC: AGENT: caderequestagentargv)>: createinstance ()>)
At./include/tac_agent_regist.h: 30
#6 0x000000000049dff2 in _ static_initialization_and_destruction_0 (_ initialize_p = 1, _ priority = 65535) at./include/tac_agent_regist.h: 51
#7 0x000000000049e62b in global constructors keyed to _ znst3tr141_global _ n_src_main.cpp_00000000_5db345946ignoree (void )()
At src/Main. cpp: 414
#8 0x0000000000530746 in _ do_global_ctors_aux ()
#9 0x0000000000000040f33b in _ Init ()
#10 0x0000000000000000 in ?? ()

It can be seen from F6 that this is a problem of static member initialization.

My code looks like this:

typedef void* (*CreateFuntion)(void);class AgentFactory{public:    static void* GetAgent(const std::string &name);    static void RegistClass(const std::string &name,CreateFuntion method);private:    static std::map<std::string,CreateFuntion> map_strName2PAgents_;};static void AgentFactoryDebug(const std::map<std::string,CreateFuntion>& _map){                                                                                                                                                std::map<std::string,CreateFuntion>::const_iterator it = _map.begin();    for (;it != _map.end();++it) {        DEBUG("Agent %s in factory", it->first.c_str());    }}std::map<std::string,CreateFuntion>   AgentFactory::map_strName2PAgents_;  void* AgentFactory::GetAgent(const std::string &name){    AgentFactoryDebug(map_strName2PAgents_);    std::map<std::string,CreateFuntion>::const_iterator it_find =         map_strName2PAgents_.find(name);    if(it_find == map_strName2PAgents_.end()){        DEBUG("Cant find agent %s", name.c_str());        return NULL;    }else{        return it_find->second();    }}void AgentFactory::RegistClass(const std::string &name,CreateFuntion method){    map_strName2PAgents_.insert(std::make_pair(name,method));}

When calling this class, the core is dark...

Run OK on other services with the same code. If it is run here, it will not work. I am a variety of crashes.

I checked some information and found the following explanation of Googler:

You cocould be coming up against the static initialization problem:

Http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.15

How are you initialising the map?

It is a way to solve the problem, so I modified the code according to this idea:

typedef void* (*CreateFuntion)(void);                                                                                                        class AgentFactory{public:    static void* GetAgent(const std::string &name);    static void RegistClass(const std::string &name,CreateFuntion method);private:    //static std::map<std::string,CreateFuntion> map_strName2PAgents_;};static std::map<std::string,CreateFuntion>& getMap(){    static std::map<std::string,CreateFuntion> __map;    return __map;}static void AgentFactoryDebug(const std::map<std::string,CreateFuntion>& _map){    std::map<std::string,CreateFuntion>::const_iterator it = _map.begin();    for (;it != _map.end();++it) {        DEBUG("Agent %s in factory", it->first.c_str());    }   }//std::map<std::string,CreateFuntion>   AgentFactory::map_strName2PAgents_;  void* AgentFactory::GetAgent(const std::string &name){    //AgentFactoryDebug(map_strName2PAgents_);    AgentFactoryDebug( getMap() );    std::map<std::string,CreateFuntion>::const_iterator it_find =         //map_strName2PAgents_.find(name);        getMap().find(name);    //if(it_find == map_strName2PAgents_.end()){    if(it_find == getMap().end()){        DEBUG("Cant find agent %s", name.c_str());        return NULL;    }else{        return it_find->second();                                                                                                                }   }void AgentFactory::RegistClass(const std::string &name,CreateFuntion method){    //map_strName2PAgents_.insert(std::make_pair(name,method));    getMap().insert(std::make_pair(name,method));}

The problem is solved.

However, it is still difficult to understand why the static members of the map are in this situation.

This post will be updated continuously tomorrow.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.