473,382 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 1358
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::staticObject = {...};
"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::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


Aug 23 '06 #4

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

Similar topics

10
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...
33
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...
7
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...
3
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...
14
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...
20
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.)...
6
by: Grey Alien | last post by:
class A { public: A(const B& ref); private: static B& b ; }; How may b be initialized ?
2
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);...
16
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; }
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.