我遇到了崩溃cxa_finalize
运行一个程序(这是一个程序,而不是其中的库):
$ ./ac-test.exe
Assertion failed: AcLock.cpp(54): AcLock
libc++abi.dylib: terminate called without an active exception
Abort trap: 6
断言/崩溃是由于对象和记录器之间的交互造成的。记录器在对象之前被破坏,但对象使用记录器。因此,正在获取的弹出断言或崩溃的互斥体已经被销毁(因此原因pthread_mutex_lock
锁定记录器时失败)。
我读过 GCC 手册指定变量的属性 http://gcc.gnu.org/onlinedocs/gcc/Variable-Attributes.html and 声明函数的属性 http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html,但我显然错过了一些东西。
我将对象和记录器放置在访问器内的公共标头中,并尝试指定构造顺序:
// 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;
}
这导致了一堆错误:
./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...
我还尝试将属性放在函数而不是变量上:
// 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;
}
这导致了更多的问题:
./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
我也尝试过__attribute__((constructor(50)))
代替init_priority
毫无喜悦。
Note:我正在苹果机器上工作。 Apple 有一个“功能”,其中构造函数优先级仅适用于同一文件中的修饰函数和变量。因此这些不能跨翻译单元分散。
准确地说,我如何指定本地静态对象的构造和销毁顺序?