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. 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
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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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;
|
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
|
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...
|
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>
|
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:
| |
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?
|
by: Grey Alien |
last post by:
class A
{
public:
A(const B& ref);
private:
static B& b ;
};
How may b be initialized ?
|
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();
|
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; }
|
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...
|
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...
| |
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,...
|
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |