Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: global static variable initialization is not working properly



Hi,
	Initialiation ordering: Function-local static objects (previously
described) are the easiest solution, but the lifetime of such objects is
undefined, i.e.  their destruction time is unspecified, sometimes *never*
destroyed.  (Related to the halting problem:  when will a program
terminate or when is the last time a particular object will be
referenced?)  Fortunately, for most programs destruction ordering at
exit-time is moot, the program's memory is released anyways, so who
cares(?).
	Initialization missing(!):  With the Mach lazy linker, an entire
module (.o) may not even be linked into the final executable at link-time
if the linker thinks a module will not be used -- this leads to missing
modules (and their static objects) at run time.  In my experience, the
linker has thought wrong on many accounts (not a bug), so to coerce
linkage, I will insert a line of code somewhere that certainly uses a
symbol in the module in question, such as a non-inline Object::Object()
constructor.  This then triggers linkage of that module at link time.
(Results evident from change in resulting binary's size!)  Note that link
*ordering* is still undefined.
	If you have an entire hierarchy of static object
initialization-ordering dependencies, you can use the function-local
static technique repeatedly with "requires-provides" relationships to make
sure dependencies are met.  Perhaps somewhat painful, sometimes necessary.

Hope this sheds some light.

> > I am facing one strange problem, in which i am not able to
> > get static object initialized completely or properly before
> > its use, and hence it is giving unexpected results.
> >
> > Is there any compile time option to force such initialization?
> > Or is there any other issue which creates such problems?
>
> Initialize the object inside a function.  Then it will always be
> initialized  before it's used.  The order in which global static
> objects are initialized isn't reliable, but objects in a function are
> always initialized before the function is called.
>
> Object& getObject() {
>    static Object object;
>    return object;
> }


David Fang
Computer Systems Laboratory
Electrical & Computer Engineering
Cornell University
http://www.csl.cornell.edu/~fang/
	-- (2400 baud? Netscape 3.0?? lynx??? No problem!)

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Unix-porting mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/unix-porting/email@hidden

This email sent to email@hidden

References: 
 >Re: global static variable initialization is not working properly (From: Stephen Langer <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.