Assuming this implementation of a singleton pattern ( of course we should avoid Singleton: it is just question), I have just been thinking about static object being created.
The classical singleton pattern does not describe the deletion aspect.
However, if I have to do it, I would start with a simple approach like the following (it is not fool-proof):
1) Similar to the static method for creating/retrieving the singleton object, say createObject()
, there is a static method for destructing the singleton object, say destructObject()
2) There is a counter which counts the current number of objects in the system;
createObject()
call, it increments by 1deleteObject()
call, it decrements by 1.
delete
is called to actually destruct the object