c++singletonstatic-variables

I have a question about using some shared resources as static variables


I'm a student who is studying game engine structure.

I've been managing shared resources through the Singleton Patterns Manager class while creating game engines. However, as the code got longer, the readability of the code became too poor. So, I changed the code thinking that the variable declaration should be made in a position where the variable is used.

First of all, there are two classes that need to be shared:

class Mesh {
public:
    Mesh() {}
    ~Mesh() {}
};
class Material {
public:
    Material() {}
    ~Material() {}
};

And the code below is the singleton pattern method used before.(singleton pattern)

class SomeManager {
public:
    static SomeManager& GetInst() {
        static SomeManager inst{};
        return inst;
    }

    Mesh* get_shared_msh() { return s_msh; }
    Material* get_shared_mtrl() { return s_mtrl; }

private:
    SomeManager()
        : s_msh()
        , s_mtrl()
    {
        s_msh = new Mesh;
        s_mtrl = new Material;

        //And another huge amount of init codes...
    }
    ~SomeManager() {
        delete s_msh;
        delete s_mtrl;

        //And another huge amount of release codes...
    }

    Mesh* s_msh;
    Material* s_mtrl;

    //... other variables
};

And the code below, it's the code I came up with.

It was hard to see the codes mixed up in one place, so I changed the shared resources used in each class to initialize in each class.

//Class that actually use these variables
class light {
public:
    light() {
        if (0 == s_instance_count) {
            init_static();
        }
        ++s_instance_count;
    }
    ~light() {
        --s_instance_count;
        if (0 == s_instance_count) {
            release_static();
        }
    }
private:
    static void init_static() {
        s_msh = new Mesh;
        s_mtrl = new Material;
    }
    static void release_static() {
        delete s_msh; s_msh = nullptr;
        delete s_mtrl; s_mtrl = nullptr;
    }

    static inline int s_instance_count = 0;
    static inline Mesh* s_msh = nullptr;
    static inline Material* s_mtrl = nullptr;
};

Question: I would like to ask programming masters if this idea is good or bad.

When I googled first before asking the question, I saw that most of the answers treated static variables as evils. However, old method is making it difficult to understand the program structure because it's so far away from where you actually use the resources and where you initialize them...


Solution

  • Your approach is very good for singleton design pattern but there is an issue you should take care of concurrency also. Use appropriate Concurrency Handling technique to make sure that your shared resource are thread safe.

    Resource Locking