Specify the order of construction / destruction of static locales in different accessories

I had a crash in cxa_finalize running a program (this is a program, not a library inside):

 $ ./ac-test.exe Assertion failed: AcLock.cpp(54): AcLock libc++abi.dylib: terminate called without an active exception Abort trap: 6 

An assertion / failure is associated with the interaction between the entity and the registrar. The logger is destroyed in front of the object, but the object uses the logger. Thus, a mutex that receives a pop-up confirmation or a failure has already been destroyed (therefore, the reason pthread_mutex_lock does not work when the registrar is blocked).

I read the GCC manual on Specifying Variable Attributes and Declaring Function Attributes , but I obviously missed something.

I placed the object and the logger in a common header inside accessors and tried to specify the construction order:

 // AcGlobals.h static AcLogger& GetLogger() { static AcLogger logger __attribute__(init_priority(50)); return logger; } static AcSocketList& GetAcceptSockets() { static AcSocketList sockets __attribute__(init_priority(100)); return sockets; } 

This led to many errors:

 ./AcGlobals.h:24:46: error: expected ';' at end of declaration static AcLogger logger __attribute__((init_priori... ./AcGlobals.h:24:47: warning: declaration does not declare anything [-Wmissing-declarations] static AcLogger logger __attribute__((init_priori... 

I also tried to place a function attribute, not a variable:

 // AcGlobals.h static AcLogger& GetLogger() __attribute__(init_priority(50)) { static AcLogger logger; return logger; } static AcSocketList& GetAcceptSockets() __attribute__(init_priority(100)) { static AcSocketList sockets; return sockets; } 

As a result, more problems appeared:

 ./AcGlobals.h:22:53: warning: GCC does not allow init_priority attribute in this position on a function definition [-Wgcc-compat] static AcLogger& GetLogger() __attribute__((init_priority(50))) { ^ ./AcGlobals.h:22:53: error: can only use 'init_priority' attribute on file-scope definitions of objects of class type 

I also tried __attribute__((constructor(50))) instead of init_priority without joy.

Note. I am working on an Apple machine. Apple has a β€œfeature” where constructor priority applies only to decorated functions and variables in a single file. Therefore, they cannot be distributed between translation units.

How to specify the order of construction and destruction of local static objects?

+3
c ++ gcc initialization clang
Dec 03 '13 at 8:52
source share
2 answers

This is a classic problem when using the Meyers singleton method (this is basically what you do). The solution is not to destroy singleton; instead of a static local variable, you should use dynamic allocation without deleting:

 static AcLogger& GetLogger() { static AcLogger* logger = new AcLogger; return *logger; } 

Please note that in this case you will need to make sure that every use the logger resets (but this is usually one way or another); otherwise, you may receive unexpanded data.

As for your attempt to use the advanced function of your compiler: I am not too familiar with this, but I do not understand how you can use something called init_priority for a local variable. The time it takes to build (and destroy) the local static variables is determined by the language (in this case, the time of destruction is not what you want). If you want to use this non-standard extension, you will probably have to make an instance variable - a member of a static class or possibly global (in this case you cannot make the constructor private).

+3
Dec 03 '13 at 9:47
source share

Assuming the dependency is non-cyclic, you can simply use the standard initialization behavior in the order you enter the code stream into the function and kill in the reverse order of initialization.

In other words, call GetLogger () to initialize the registrar, and then GetAcceptSockets () to initialize the list. This will cause the list of sockets to be destroyed first (while the logger still exists), and then the last one will be destroyed.

+1
Dec 03 '13 at 9:00
source share



All Articles