C++: Difference between revisions

Jump to navigation Jump to search
1,086 bytes removed ,  8 years ago
[unchecked revision][unchecked revision]
Line 59:
 
Note: This appears to be specific to the Itanium platform. For IA-32/x86/i386 and amd64/x86_64, please check out [[Calling_Global_Constructors]] instead.
 
Note: This section does not discuss how to perform the same action from your own ELF linker. If you are providing your own environment, look for the .ctors and .dtors sections as these each contain a list of pointers to each constructor / destructor that must be called. Although most things seem to work without support for global constructors, there is one pattern that will segfault with GCC. Specifically:
 
<source lang="cpp">
class A
{
public:
A() {}
}
 
A g_a;
 
void foo(void)
{
A *p_a = &g_a;
p_a->anything(); // <---- segfault
}
</source>
 
It appears that GCC is using the constructor / destructor initialization routines to do more than simply call the constructors / destructors of each globally defined class. Executing the functions defined in .ctors / .dtors not only initializes all of the constructors / destructors, but resolves these types of segfaults (the above is only one example of many that are resolved). See the following for more info: [https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68738]
 
 
According to the [http://refspecs.freestandards.org/LSB_3.1.0/LSB-Core-generic/LSB-Core-generic/baselib---cxa-atexit.html Itanium C++ Application Binary Interface] (which '''g++''' follows and VC++ does not) the function '''__cxa_atexit''' is used to register a destructor that should be called when a shared library needs to be unloaded. It should insert a function pointer with maximum 1 accompanying argument and the handle of the object or shared resource to be destroyed into a table.
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.

Navigation menu