Skip to Main Content

DevOps, CI/CD and Automation

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to order C++ object creation

12e13536-03da-465d-890f-e2589cde2e9fMar 22 2017 — edited Mar 24 2017

I'm working with a cross-platform C++ library. The library has a handful of global objects that are sensitive to the order of construction and destruction. When I say "handful", I mean 3 to 6, depending on the architecture. I also realize the C++ language makes no accommodations for this use case, and we have to turn to specific platform extensions to control the problem. This is often referred to as the C++ Static Initialization Order Fiasco.

Linux and Microsoft platforms provide a way to order global C++ object creation. Under Linux, we can use GCC's init_priority to insert object construction into the dynamic initializer list in a particular order. On Microsoft platforms, we can use Microsoft's init_seg(lib) to control C++ object creation after the CRT is initialized (but before regular user objects are created). A nice feature of Linux and Microsoft platforms is we can do it from source code, and we don't need to provide custom scripts.

At the moment we don't have anything Oracle/Sun platforms. Programs created with both GCC and Sun Studio's CC order the object files that are fed to the linker, but that depends on undocumented behavior. We would like to use a more deterministic method like init_priority or init_seg.

I have two questions. First, is it possible to control the order of C++ object creation on Oracle/Sun platforms? Second, if so, then how do we insert objects into the C++ dynamic initializer list in a particular order on Oracle/Sun platforms?

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 21 2017
Added on Mar 22 2017
4 comments
1,086 views