473,811 Members | 3,039 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Static object not constructed by loader: ctor not called?

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.
Aug 23 '06 #1
3 1390
noleander wrote:
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
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
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.
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
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.
-cd


Aug 23 '06 #2
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::sta ticObject = {...};
"Carl Daniel [VC++ MVP]" wrote:
noleander wrote:
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.
Aug 23 '06 #3
noleander wrote:
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::sta ticObject = {...};
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


Aug 23 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
2771
by: Grahamo | last post by:
Hi, I have a a routine that is frequently called. It simply iterates over a list of dates and does some sanity checking. The following pseudo code summarises it. void check() { const Date someDate = 1/1/2005;
33
3361
by: Chris Capel | last post by:
What is the rationale behind the decision not to allow abstract static class members? It doesn't seem like it's a logically contradictory concept, or that the implementation would be difficult or near-impossible. It seems like it would be useful. In fact, there's a place in my code that I could make good use of it. So why not? Chris
7
2431
by: Adam | last post by:
I have a managed cpp wrapper. Im using this in a native dll as a static variable. I need to free this library when the dll is done being used. The perfect place to do this is DllMain for DLL_PROCESS_DETACH, but I can't do this when touching managed code, even if its just calling "delete someObject;". Any recommendations on how to free this object (it needs to be freed when the dll is being unloaded from its caller). Since the CRT is...
3
2252
by: tropos | last post by:
(Platform: Solaris with gmake and native Sun C++ compiler) Problem: If I create a shared object (.so file) and load it into a executable, the loader correctly runs constructors of static objects in the .so file. But if I link the same code statically, with no shared object, then the constructors don't run at all! Why?? Here's an example: <file AnnounceConstruction.cpp>
14
2592
by: Jeroen | last post by:
Hi all, I've got a question about writing a library. Let me characterize that library by the following: * there is a class A which is available to the user * there is a class B that is used in severel 'underwater operations' * there is a list which stores objects of class B There are several issues I'm not sure about:
20
6104
by: JohnQ | last post by:
The way I understand the startup of a C++ program is: A.) The stuff that happens before the entry point. B.) The stuff that happens between the entry point and the calling of main(). C.) main(). So, if the above is OK, does static initialization occur during A or B? What happens during A?
6
3256
by: Grey Alien | last post by:
class A { public: A(const B& ref); private: static B& b ; }; How may b be initialized ?
2
1906
by: .rhavin grobert | last post by:
i have (do try to have?) the following... & = breakpoints in debugger // ---------------------------------------------------------------- // cx.h class CX { public: CX(CX* pcx = NULL); virtual ~CX();
16
3011
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; class Test { static Test t; static Test init_Test( ) { return t; }
0
10662
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10398
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10416
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10138
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7676
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6897
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5702
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3881
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3028
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.