If a C++ class with nontrivial constructors and destructors is instantiated globally in a shared library, is its constructor called when the library is loaded, and similarly, is its destructor called when the library is unloaded? Is this a guarantee, or does it just usually happen? Can it ever not happen?
            Asked
            
        
        
            Active
            
        
            Viewed 73 times
        
    2
            
            
        - 
                    I believe shared library behaviour is platform specific and depends on the ABI used. For ELF defines ctor and dtor list, and the system should respect them. – Non-maskable Interrupt Aug 24 '14 at 14:33
 - 
                    Related explanations: http://stackoverflow.com/questions/19373061/what-happens-to-global-and-static-variables-in-a-shared-library-when-it-is-dynam and http://stackoverflow.com/questions/75701/what-happens-to-global-variables-declared-in-a-dll – masoud Aug 24 '14 at 14:43
 
1 Answers
1
            
            
        Yes, the constructors and destructors will always be called when the library is loaded or unloaded.
        Samir Jindel
        
- 71
 - 4
 
- 
                    Can you cite a source for this? I don't even remember why I was asking this, it was almost a year ago. But I'd still like to know. – Wug Mar 16 '15 at 23:05