Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6837713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:30:36+00:00 2026-05-26T23:30:36+00:00

I am trying to implement factory pattern by registering the function pointers of the

  • 0

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.

Code Snippet:

factory.cpp

typedef Shape* (*Funcptr)();

std::map<int,Funcptr> Factory::funcmap;

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

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

factory.h

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

Square.cpp

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

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:30:37+00:00Added an answer on May 26, 2026 at 11:30 pm

    There is no guarantee about the order of creation of static objects that are defined in different translations uints* so you have a 50/50 shot as to which would happen first, the initializtion of Factory::funcmap or Factory::registerCreator(1,SquareCreator) and Undefined Behavior Russian Roulette is not a good game to play.

    A common approach to deal with this, and one that described in Item 4 of the third edition of Scott Meyer’s Effective C++ is to use local static objects instead of global static objects. In this case it means changing Factory to look like this:

    class Factory { 
    public: 
        Factory(); 
        virtual ~Factory(); 
        static int registerCreator(int, Shape *(*CFuncptr)()); 
        Shape* CreateObject(int); 
    private: 
        static std::map<int,Funcptr> & GetFactoryMap() {
            static std::map<int,Funcptr> funcmap;
            return funcmap;
        } 
    }; 
    

    and changing Factory::registerCreator to this:

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

    This way funcmap will be initialized the first time registerCreator is called and will never be used uninitialized.

    *Or, roughly speaking, different .cpp files if you are not familar with the term translation unit

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to implement a factory class that generates objects and intercept all
I'm trying to implement what I believe is a factory class. I have an
I am trying to serialize/deserialize objects that have factory-created members. For example, lets say
I'm trying to create an EJB factory class, which works like this: You have
I am trying to implement a factory for two classes Circle, Square both of
I am trying to implement a logging factory and I have used an interface
I'm currently trying to implement a factory as a singleton. I practically used the
I have been reading about Factory pattern a lot lately. I am trying to
I'm having a hard time figuring out how to implement a factory pattern in
All I am currently trying implement something along the lines of dim l_stuff as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.