问题描述
这似乎pretty清楚,它应该设置的东西了。
- 在当前正是它运行?
- 为什么有两个括号?
- 是
__ __属性
函数?宏?语法? - 请问用C这项工作? C ++?
- 是否与工作需要的功能是静态的?
- 什么时候
__ __属性((析构函数))
运行?
Example在目标C :
__ __属性((构造函数))
静态无效initialize_navigationBarImages(){
navigationBarImages = [[的NSMutableDictionary的alloc]初始化];
}__attribute __((析构函数))
静态无效destroy_navigationBarImages(){
[navigationBarImages发布]
}
- 它时加载的共享库运行,通常在程序启动。
- 这是所有GCC属性如何; presumably从函数调用区别开来。
- 针对GCC-语法。
- 是
- 无。
- 的析构当共享库卸载运行,通常在程序退出。
所以,一路构造函数和析构函数的工作是共享对象文件包含特殊路段包含分别标有构造和析构的属性,该函数的引用(ELF上和.ctors .dtors)。当库加载/卸载的动态装载程序(ld.so或诸如此类)检查是否存在这样的部分,并且如果是的话,呼叫参考在其中的功能。
试想想起来了,有可能是正常的静态连接一些类似的魔法,使同一code是启动/关机运行,如果不考虑用户选择静态或动态链接。
It seems pretty clear that it is supposed to set things up.
- When exactly does it run?
- Why are there two parentheses?
- Is
__attribute__
a function? A macro? Syntax? - Does this work in C? C++?
- Does the function it works with need to be static?
- When does
__attribute__((destructor))
run?
__attribute__((constructor))
static void initialize_navigationBarImages() {
navigationBarImages = [[NSMutableDictionary alloc] init];
}
__attribute__((destructor))
static void destroy_navigationBarImages() {
[navigationBarImages release];
}
- It's run when a shared library is loaded, typically during program startup.
- That's how all GCC attributes are; presumably to distinguish them from function calls.
- GCC-specific syntax.
- Yes.
- No.
- The destructor is run when the shared library is unloaded, typically at program exit.
So, the way the constructors and destructors work is that the shared object file contains special sections (.ctors and .dtors on ELF) which contain references to the functions marked with the constructor and destructor attributes, respectively. When the library is loaded/unloaded the dynamic loader program (ld.so or somesuch) checks whether such sections exist, and if so, calls the functions referenced therein.
Come to think of it, there is probably some similar magic in the normal static linker, so that the same code is run on startup/shutdown regardless if the user chooses static or dynamic linking.
这篇关于究竟如何__attribute __((构造函数))工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!