Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / August 2006

Tip: Looking for answers? Try searching our database.

Static object not constructed by loader: ctor not called?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
noleander - 23 Aug 2006 05:20 GMT
Im getting a runtime error because Ive got a static object that is not
properly initialized.  

---------- header file xxx.hpp ---------------
class SomeClass {
 SomeClass() { .. constructor here ..;}
} // end SomeClass
static  SomeClass  staticObject;  // declaration in header

--------------  in .cpp file: ----
SomeClass staticObject;  // definition in .cpp file: constructor called by
loader

When the code is executed at run time, and the static  object staticObject
is accessed, I get a failure becuase it's constructor has not been executed
yet (so its data members are not initialized).

Another programmer on my team says that statics (and globals) are okay for
integers, floats, simple arrays (of ints, floats), char[] strings, and for
classes (or structs) that DO NOT have a constructor.

But, he says, one should never use a static/global for an object of a class
that has a constructor, because the loader is very unpredictable, and you
cannot be sure the loader will call the static's constructor before some
software tries to use/access the static.

Can anyone confirm this?  Does anyone else have problems using
statics/globals of classes that have constructors.

PS:  I should mention that this software is in a DLL (not an application)
that is attached to processes at run time, not link time.
Carl Daniel [VC++ MVP] - 23 Aug 2006 06:39 GMT
> Im getting a runtime error because Ive got a static object that is not
> properly initialized.
[quoted text clipped - 4 lines]
> } // end SomeClass
> static  SomeClass  staticObject;  // declaration in header

That's probably not what you wanted.  Change this to

extern SomeClass staticObject;

As is, you're creating an instance of the object in every translation unit
that includes the header.

> --------------  in .cpp file: ----
> SomeClass staticObject;  // definition in .cpp file: constructor
[quoted text clipped - 7 lines]
> okay for integers, floats, simple arrays (of ints, floats), char[]
> strings, and for classes (or structs) that DO NOT have a constructor.

Globals/statics that have constructors are perfectly fine, but you have to
be careful when accessing one static object from within the constructor of
another static object as the order of initialization guarantees provided by
the C++ language specification are well... weak.

> But, he says, one should never use a static/global for an object of a
> class that has a constructor, because the loader is very
[quoted text clipped - 7 lines]
> PS:  I should mention that this software is in a DLL (not an
> application) that is attached to processes at run time, not link time.

-cd
noleander - 23 Aug 2006 15:36 GMT
Thanks for the tip.   The actual code I have (my snippet above simplified
things) is:

---------- header file xxx.hpp ---------------
class SomeClass {
  SomeClass() { .. constructor here ..;}
} // end SomeClass

class OtherClass {
   static  SomeClass  staticObject;  //  static data member
   ....
}

----------  xxx.cpp file -----------
static OtherClass::staticObject = {...};

>  > That's probably not what you wanted.  Change this to
>
> extern SomeClass staticObject;
>
> As is, you're creating an instance of the object in every translation unit
> that includes the header.
Carl Daniel [VC++ MVP] - 23 Aug 2006 15:48 GMT
> Thanks for the tip.   The actual code I have (my snippet above
> simplified things) is:
[quoted text clipped - 11 lines]
> ----------  xxx.cpp file -----------
> static OtherClass::staticObject = {...};

Oh, OK.  That's completely different and should be fine.  Constructors for
such objects definitely will run, but the order of constructor execution
between globals decalred in different translation units is undefined.  If
you're entering this object from another global object constructor in a
different translation unit, you may well touch this object before it's
constructor is run.

-cd

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.