🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.oodesign
Article #122

Re: object create order

#122
From: Chris Uzdavinis
Date: Thu, 11 Nov 2004 11:44
47 lines
3521 bytes
Sten Larsson <stenX@Xstenl.com> writes:

> Yes - I know... I just want to use some trick to guarantee that
> objects exist when I need them too. As far as I remember, with these
> global objects:
>
> string a;
> string b;
>
> There is really no way of knowing if a or b is created first (by
> reading the C++ standard), and this pretty much happens before the
> main thread kicks in (well, at least before main() is called.

If they are declared in the same source file, then their
initialization order is guaranteed to be in order of declaration.  If
they're declared in different source files that are later linked
together, then the order of initialization is indeterminate.

> Creating the objec using "new" instead of a local static wouldn't
> really improve things... right? I'm sure it would be a different error
> behaviour instead.

Actually it can help.  The much-maligned "singleton" pattern can help
with order-of-initialization problems, by creating the object on an
as-needed basis.  Thus, if we're creating singleton A and it uses
singleton B in its constructor, then creation A waits for the creation
of B to complete, and then A's construction continues, and so on.

One big problem of singleton pattern is cyclic dependencies, and if
you have a maze of different variables all initialized in different
files who all make use of each other, most likely it'll still fail,
because one singleton may make use of another singleton that is still
in the process of constructing, but that would cause another instance
to begin constructing, potentially creating an infinite recursive loop
until the program crashes.  Cyclic-dependencies are not a problem
specific to singletons at all, though.  It's just that singletons do
not help solve that problem.

Another problem with singletons is initialization in multi-threaded
applications is nearly impossible to do safely if you allow threads to
begin running before main (such as spawning them from the constructor
of a global variable.)  Double-checked locking attempts to help solve
the initialization problem in multiple threads in an efficient manner,
but has been shown to not work as well as desired.

-- 
Chris (TeamB);
Technical Details
Message-ID: <j5r7n0fw4c.fsf@explicit.atdesk.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!fr.ip.ndsoftware.net!border2.nntp.ams.giganews.com!nntp.giganews.com!news-hub.cableinet.net!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail
References: <kcdqo0t2f0vkit49u0l9goji42stukiq17@4ax.com> <418ed304$1@newsgroups.borland.com> <g2uto0p62cvgkccrnnd13dke6vf19d9m94@4ax.com> <adu3p0ha0a5ur21ssd817c6ugqegigee72@4ax.com> <cgs4p0pdn91rqd3f9gq16po2481g2dqm86@4ax.com> <r6t4p09hq6msmtm862t76ohmk6et7fg6di@4ax.com> <j5zn1pqq9i.fsf@explicit.atdesk.com> <r086p0l46hi788h2208h1hf0egp14p9hvp@4ax.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10db1e,7fd39362bd6d0fe2
X-Google-Attributes: gid10db1e,public
X-Google-Language: ENGLISH,ASCII-7-bit
Newsgroups: borland.public.cppbuilder.oodesign
Subject: Re: object create order
References: <kcdqo0t2f0vkit49u0l9goji42stukiq17@4ax.com> <418ed304$1@newsgroups.borland.com>
 <g2uto0p62cvgkccrnnd13dke6vf19d9m94@4ax.com>
 <adu3p0ha0a5ur21ssd817c6ugqegigee72@4ax.com>
 <cgs4p0pdn91rqd3f9gq16po2481g2dqm86@4ax.com>
 <r6t4p09hq6msmtm862t76ohmk6et7fg6di@4ax.com>
 <j5zn1pqq9i.fsf@explicit.atdesk.com>
 <r086p0l46hi788h2208h1hf0egp14p9hvp@4ax.com>
From: Chris Uzdavinis (TeamB) <chris@uzdavinis.com>
Organization: TeamB
Date: Thu, 11 Nov 2004 06:44:35 -0500
Message-ID: <j5r7n0fw4c.fsf@explicit.atdesk.com>
User-Agent: Gnus/5.1002 (Gnus v5.10.2) Emacs/21.2 (gnu/linux)
Cancel-Lock: sha1:VBQZY2BNZY09WdtrokyRhHjRNK4=
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
NNTP-Posting-Host: 63.68.0.194
X-Trace: newsgroups.borland.com 1100173475 63.68.0.194 (11 Nov 2004 03:44:35 -0700)
Lines: 47
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!fr.ip.ndsoftware.net!border2.nntp.ams.giganews.com!nntp.giganews.com!news-hub.cableinet.net!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail