Properly deleting a singleton

前端 未结 5 832
悲&欢浪女
悲&欢浪女 2021-01-24 23:07

I have the following code:

MyClass.h:

static MyMutex instanceMutex;
static MyClass* getInstance();
static void deleteInstance();

MyClas

5条回答
  •  借酒劲吻你
    2021-01-24 23:37

    getInstance and delteInstance should be static, so they can only work with static members of the class. The static members don't get destroyed with the instance.

    If your code is multithreaded, the code is not safe. There's no way to make sure there's no pointer to the instance held in some running context.

提交回复
热议问题