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 7739601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:33:52+00:00 2026-06-01T08:33:52+00:00

I have two singletons, the first one’s header looks like this (I’ve omitted everything

  • 0

I have two singletons, the first one’s header looks like this (I’ve omitted everything that is not related to the Singleton pattern):

#ifndef TEXTUREMANAGER_DEFINED_H
#define TEXTUREMANAGER_DEFINED_H
class FontManager;
class TextureManager
{
private:
    static TextureManager *instance;
    TextureManager();
public:
    FontManager *fontManager;
    static TextureManager* Instance();
};

#endif

And in the implementation, this is the Instance() method (and the initialization of the instance static member):

#include "FontManager.h"
TextureManager * TextureManager::instance = 0;
TextureManager* TextureManager::Instance ()
{
    if (instance==0)
        instance=new TextureManager;
    return instance;
}

And this is the constructor:

TextureManager::TextureManager()
{
    fontManager=FontManager::Instance();
}

The second singleton’s (FontManager’s) design is exactly the same, but instead of a FontManager pointer has a TextureManager pointer, and in it’s constructor it initializes that pointer with TextureManager::Instance().
This should work like this: TextureManager is instanciated first (When the program starts), and in its constructor instanciates for the first time the FontManager singleton calling FontManager::Instance(). FontManager, in it’s constructor, assigns its pointer to a TextureManager with TextureManager::Instance() and this method returns the TextureManager instance that already exists. Right?

But instead of that the program enters an infinite loop because (I don’t know why) the Instance() methods always create a new instance. I’ts like the if (instance==0) always evaluated to true.

  • 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-06-01T08:33:53+00:00Added an answer on June 1, 2026 at 8:33 am

    Because you’ve written an infinite loop where the constructor of TextureManager calls the constructor of FontManager which then calls the constructor of TextureManager …. and so on.

    Because the constructor has to complete before the static variables are assigned you’ll end up in this loop.

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

Sidebar

Related Questions

I have two services, one that calls another. Both are marked as singletons as
I have two enums that cross reference each other. Each one has a constructor
Have two actionsheet buttons and one modalviewcontroller on mainviewcontroller in application. Now for two
I have two applications written in Java that communicate with each other using XML
I have two projects, one of them is a Windows phone application and the
I have two views in my app, one is a general view where CoreLocation
I have an application in java that uses two different threads.And for sharing data
Suppose I have two types, TypeA and TypeB, that I want to register with
I have class that I believe should not be a singleton or static class.
I have two tomcat web applications that need to share information using a singleton.

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.