When is a singleton destructor called




















If your application calls for multiple, dependent singletons, then you may have to revert to explicit destruction. One thing's for sure: you can't use more than one destroyer if the singleton destructors depend on one another. The draft standard promises a lot: The function atexit from can be used to specify a function to be called at exit. If atexit is to be called, the implementation shall not destroy objects initialized before an atexit call until after the function specified in the atexit call has been called.

This suggests that classes having static instances should avoid depending on singletons during destruction. Or at least there should be a way for such classes to check for the existence of the Singleton during destruction. While this obviates the need for destroyers, the real problem — deleting mutually-dependent singletons — remains. Garbage collection, anyone? Long ago, in a galaxy far, far away, Scott Meyers posited the following: My version of Singleton is quite similar to yours, but instead of using a class static and having Instance return a pointer, I use a function static and return a reference:.

This seems to offer every advantage your solution does no construction if never used, no dependence on initialization order between translation units, etc.

Furthermore, my solution makes it much less likely a caller will inadvertently delete the singleton in a misguided attempt to avoid a memory leak. Am I overlooking a reason for returning a pointer to a class static instead of a reference to a function static? The only drawback I could see here is that it makes it hard to extend the Singleton through subclassing, since Instance will always create an object of type Singleton.

Besides, one needn't worry about deleting the Singleton instance if its destructor isn't public. While I have since developed a slight preference for returning a reference, in the end it seems to make little difference.

Later, Erich Gamma noticed a more fundamental problem with this approach: It turns out that it is not possible to make thread-safe if multiple threads can call Instance. In such situations you would have to acquire the lock at the call site — pretty ugly. In this column, John talks about Singleton in the context of protection for multi-user file systems.

Ironically, we'd been having some mysterious problems recently with memory leaks on multi-threaded versions of ACE on multi-processors. As I read John's column, it suddenly struck me that the problem was caused by multiply initialized Singletons due to race conditions. Once I saw the connection between the two issues, and factored in the key forces e.

About a month later, Doug sent me a follow-up:. The nice thing about it is that it automates the Double-Check pattern and also enables easy parameterization of the LOCK strategy.

I've enclosed it below, just for fun. I was intrigued, especially the part about being "quasi-useful. His response was illuminating, and a little unnerving: Right, exactly. In this case you have to implement the static instance method like this:.

That, in turn, fired some of my dormant neurons. I wrote back that if you really wanted to make a class inherently singleton, you could subclass it from this template, passing the subclass as a template parameter Cope's curiously recurring template pattern again — I love it! For example:. That way, you would preserve Singleton semantics without recoding the pattern in all its multi-threaded gory sic. Caution: I haven't tried out this variation myself, nor have I had occasion to use it.

I just think it's neat, both aesthetically and in the way we arrived at it. I used to think Singleton was one of the more trivial of our patterns, hardly worthy to hobnob with the likes of Composite, Visitor, etc.

Boy, was I wrong! After 3 years of work, we've finally released a new ebook on design patterns! Check it out ». The All-Important Destructor Like any self-respecting class, a Singleton class should define a destructor. The object instance will be destroyed, and thus, it's destructor will get called at program termination because it's static.

Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 4 years, 6 months ago. Active 4 years, 6 months ago.

Viewed 3k times. Improve this question. Why would a destructor be called here? You just created a reference which then got destroyed. References are not proper objects and don't have destructors. If you're using a global variable or static variable, the destructor will be called, assuming the program exits normally. Make sure that GetSingleton returns a pointer to type Singleton. The constructor of singleton class would be private so there must be another way to get the instance of that class.

This problem is resolved using a class member instance and a factory method to return the class member. While static initialization is performed prior to any execution of code, the order in which initialization is In my company we make the destructor protected and then provide a static public function to delete the singleton object, the in same way that you normally provide a static public function to allocate and gain access to the object.

Singletons, It doesn't have anything to do with destructors or deleting an instance. When and how to delete your singleton is a very good question though, Destructors in Java can be learned with the finalize method in Java.

The concept is as same as the finalize method. Java works for all except the destructor with the help of Garbage collection. Therefore in case, there is a need for calling the destructor, it can be done with the help of the finalize method.

This method is not independent as it. Singleton Destructors. You can rely on it being cleaned up by the operating system. Regardless, when the process ends, all memory returns to the operating system. RAII will help you here. You should explicitly clean up all your objects. Never rely on the OS to clean up for you. How are you creating the object? If you rely on the runtime library destroying your static objects after main returns, and you hope for it to be possible to use the code in a Windows DLL, then you are running code during DllMain, and most things you might like to do are unsafe.

It's not folklore if you write reusable code as part of an application development effort.



0コメント

  • 1000 / 1000