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

  • SEARCH
  • Home
  • 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 6687325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:16:32+00:00 2026-05-26T05:16:32+00:00

I ran into a weird problem today that I don’t really fully understand. Hopefully

  • 0

I ran into a weird problem today that I don’t really fully understand. Hopefully someone here can help.

The setup is fairly easy. I have a class that has a static member of type std::set. The class has 2 template constructors that differ in the number of arguments only. It’s the same behavior for both constructors, so just note that the constructor is templatized, and the constructor is searching and inserting into the std::set.

I’m experiencing the following behavior:
For a static instance of the class, the constructor crashes on the first method called on the static std::set (find()). It looks like the set is not initialized. It seems to me like the constructor is being called before the static member variable is initialized.

Here is a simplified example:

////////// Header File

class ConVar : public IListener
{
  friend EventHandler; // Event Handler auto registers all instances of convar to commands

public: // Auto

  template< typename T >
  ConVar(string const& name, string const& description, T const& default_value );

private:
  static std::set<u32> mRegisteredVars;
};


//////// INL file (included from header)

template< typename T >
ConVar::ConVar(string const& name, string const& description, T const& default_value )
  : mName(name), 
    mhName(name),
    mDescription(description),
    mClamp(false)
 {
    u32 hname = CONSTHASH(name.c_str());
    ErrorIf(mRegisteredVars.find(hname) != mRegisteredVars.end(), "Attempt to create same ConVar multiple times. Note the ConVars are static singletons!");

    *this = default_value;

    mRegisteredVars.insert(hname);

    gCore.Events.Subscribe(mhName, this);
  }

   ///////////// .cpp file

  std::set<u32> ConVar::mRegisteredVars;

The crash occurs inside of the ErrorIf on the find method. If I comment that line it crashes on the line where it inserts.

The constructor is called before main (static instance of the class)
Does anyone know what might be going on here?

  • 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-26T05:16:33+00:00Added an answer on May 26, 2026 at 5:16 am

    Global objects that access each other from the constructor are going to have problems with the order of their instanciation.

    There are a couple of ways around this:

    Try

    //Change
    static std::set<u32> mRegisteredVars;
    
    //Into
    static std::set<u32>&  getRegisteredVarsSet()
    {
        static  std::set<u32>&  mRegisteredVars;
        return mRegisteredVars;
    }
    // Obviously remove the `std::set<u32> ConVar::mRegisteredVars;`
    // From the cpp file.
    

    Then wherever you use: mRegisteredVars change to getRegisteredVarsSet()

    Now even if you access mRegisteredVars from the constructor of a static storage duration object the call getRegisteredVarsSet() (to retrieve it) will guarantee that mRegisteredVars will be fully initialized before it is returned and thus available for use.

    Because it is a static member of the function its lifespan is the length of the program thus it will retain its state between calls.

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

Sidebar

Related Questions

Today, I ran into this weird problem with a user using Mac OS X.
I ran into a very weird problem today. Long story short, my function returns
I ran into a weird problem while trying to build a Traits GUI that
I ran into a weird behavior from Oracle this morning... And I can't understand
Ran into this problem today, posting in case someone else has the same issue.
I've ran into a really weird problem. I am building a heavily distributed application
I'm developing an iPad app and I ran into a really weird issue here.
I've ran into a really weird problem while working on a large project. I
I ran into the problem that my primary key sequence is not in sync
Ran into some weird behaviour and wondering if anyone else can confirm what I

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.