accessing a static map from a static member function - segmentation fault - C++
<p>I am trying to implement factory pattern by registering the function  pointers of the derived class to the factory in a static map(member of the factory)and creating objects by looking up the map. But I am getting a segmentation fault in doing this. </p>

<p>Code Snippet:</p>

<p>factory.cpp</p>

<pre><code>typedef Shape* (*Funcptr)();

std::map&lt;int,Funcptr&gt; Factory::funcmap;

int Factory::registerCreator(int ShapeID, Shape *(*CFuncptr)()) {
    Factory::funcmap[ShapeID] = CFuncptr;
return 1;
} 

Shape* Factory::CreateObject(int ShapeID) {
    std::map&lt;int,Funcptr&gt;::iterator iter;
    iter = funcmap.find(ShapeID);
    if(iter != funcmap.end()){
        return iter-&gt;second();
    }
    return NULL;
}
</code></pre>

<p>factory.h</p>

<pre><code>class Factory {
public:
    Factory();
    virtual ~Factory();
    static int registerCreator(int, Shape *(*CFuncptr)());
    Shape* CreateObject(int);
private:
    static  std::map&lt;int,Funcptr&gt; funcmap;
};
</code></pre>

<p>Square.cpp</p>

<pre><code>static Shape *SquareCreator() { 
    return new Square; 
}
static int SquareAutoRegHook = Factory::registerCreator(1,SquareCreator);
</code></pre>

<p>On creating the object for Factory in the main file a segmentation fault occurs. 
Can you please suggest if I am doing something wrong. I am using CppUTest for TDD and not sure how to debug this.</p>
