When writing C++ with singletons, it’s natural to write code like this:

​​

The static member function getInstance gets the singleton pointer and does some finishing work in the destructor.

Can a singleton pattern be written wrong when you run code and find that the destructor never executes? Check again and again and find no problem, so check StackOverflow to find the cause. I happen to have a buddy who has the same question, and a buddy has a feasible solution. Modify the code based on its answer as follows:

​​

​​

The main change from the previous code is to remove the static pointer member and replace it with a static member within the function. Since _instance is a static member within the function, it is initialized on the first call (thanks to the no-argument constructor), subsequent calls skip initialization and execute subsequent code; The function returns a reference to an instance, so you get the same object each time you call it, fulfilling the singleton purpose. After program execution, the instance’s destructor is automatically called, and the code in the destructor executes correctly.

The problem is solved, but why is the destructor for the first singleton not executing?

This is due to the way C++ holds objects (or allows programmers to manually control memory). Java/PHP and other languages with a recycling mechanism hold objects through Pointers. After programmers apply for objects, memory will be automatically allocated, and the system is responsible for tracking and recycling useless objects and their existence. C++ allows developers to hold objects as variables, for example: Foo Foo [= Foo(args)]. After the variable is initialized, a reference to the object is obtained. After leaving scope, the system destroys the execution stack and the object is automatically destructed. C++ can also get a reference to an object as a pointer: Foo* Foo = new Foo(args). Objects allocated in this way need to be managed manually by developers. If delete is not performed, the object and allocated memory remain until the program exits and is reclaimed by the operating system. The following code illustrates this:

​​

​​

As you can see from the above code, the compiler and the system are afraid to reclaim the memory of an object created by new without reference counting. Multiple Pointers to the same object may cause other code to crash if the object is deleted. After the memory is released, other Pointers delete the same memory multiple times, causing unpredictable risks.

In conclusion, the reason pointer singleton destructors are not called is that objects that are new need to be deleted themselves, and don’t expect others to call the destructor correctly for you.

The solutions to the problem are as follows:

(1) Use a variable to hold singletons on StackOverflow. All variables are destroyed at the end of the program, and the destructor of variables is called correctly.

(2) Delete singleton before main exits. For example, add a static member function destroy to destroy the object to which the pointer points.

(3) Use smart Pointers such as auto_Ptr/unique_pTR.

If there are other solutions, welcome to exchange correction!

So today’s share here, the follow-up will update more exciting projects or knowledge content, you have to learn C language C++ yo ~

Wechat official account: C language programming Learning base