I think I am running into the static initialization problem but I do
not understand why.
I am trying to parse a configuration file. To make this parser generic
I register callbacks for various section keywords in the configuration
file. I want to share this map of callbacks across multiple instances
of the config file (for example, when I merge two files). Whenever I
introduce a new section in the configuration file, I define a new class
for this section and also register the new callback for this section
with a new keyword.
I get an access violation exception when I step through this code and I
see the callback map Root() is uninitialized. Since I am trying to
populate the callback map only after constructing an instance of the
configuration class, why is the static callback map still
uninitialized?
Below is the basic code structure:
-- In CConfig.h
class CConfig
{
public:
...
// Note that Register is not a static function
bool Register(const std::string& skey, CallbackFn callback);
private:
// want to share this map across all instances
static std::map<std::s tring, CallbackFn> s_callbacks;
};
-- In CCMA.h
class CCMA : public CSection
{
...
};
-- In CCMA.cpp
// anonymous namespace
namespace
{
// ReadCMA -> new CCMA and then read relevant tags from config file
bool bRegisterCMA = G_CONFIG.Regist er("CMA_START" , ReadCMA);
};
G_CONFIG returns either existing global pointer to CConfig after
creating a new CConfig object if necessary simulating singleton
behavior. However, we sometimes need more than one instance of CConfig
for example to merge entries from 2 different configuration files and
so we access these without using G_CONFIG (I still want to share the
callback map).
Thanks 4 2536
marvind wrote: I think I am running into the static initialization problem but I do not understand why.
I am trying to parse a configuration file. To make this parser generic I register callbacks for various section keywords in the configuration file. I want to share this map of callbacks across multiple instances of the config file (for example, when I merge two files). Whenever I introduce a new section in the configuration file, I define a new class for this section and also register the new callback for this section with a new keyword.
I get an access violation exception when I step through this code and I see the callback map Root() is uninitialized. Since I am trying to populate the callback map only after constructing an instance of the configuration class, why is the static callback map still uninitialized?
Below is the basic code structure:
-- In CConfig.h class CConfig { public: ... // Note that Register is not a static function bool Register(const std::string& skey, CallbackFn callback); private: // want to share this map across all instances static std::map<std::s tring, CallbackFn> s_callbacks; };
-- In CCMA.h class CCMA : public CSection { ... };
-- In CCMA.cpp // anonymous namespace namespace { // ReadCMA -> new CCMA and then read relevant tags from config file bool bRegisterCMA = G_CONFIG.Regist er("CMA_START" , ReadCMA); };
G_CONFIG returns either existing global pointer to CConfig after creating a new CConfig object if necessary simulating singleton behavior. However, we sometimes need more than one instance of CConfig for example to merge entries from 2 different configuration files and so we access these without using G_CONFIG (I still want to share the callback map).
Thanks
You need to make sure that "s_callback s" is initialized before
"bRegisterC MA" is. One way to do so would be to grab a proper
Sincleton implementation, and make "s_callback s" a singleto too.
The simple solution for single-threaded programs is to put all your
static data in functions that return references to the data. That way
you make sure that they are constructed in the right order.
And: you get the problem because "s_callback s" is not initialized with
the first instance of the class, but along with all other static data.
Nonconst static members are just "globals in a namespace".
Thank you for your response.
I wrapped the callback map as you suggested:
stlCallbackMap& CallbackMap()
{
static stlCallbackMap callbackMap;
return callbackMap;
}
That "seems" to work. My mistake was that I thought that static data
members of a class are initialized before the first instance of the
class is constructed, instead of the runtime treating them as just
globals in a namespace.
Are there good "free" tools that I can use to find out if there are
such errors in the code since these errors depend on how the files are
compiled?
"marvind" <ma********@yah oo.com> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.com.. . I think I am running into the static initialization problem but I do not understand why.
I am trying to parse a configuration file. To make this parser generic I register callbacks for various section keywords in the configuration file. I want to share this map of callbacks across multiple instances of the config file (for example, when I merge two files). Whenever I introduce a new section in the configuration file, I define a new class for this section and also register the new callback for this section with a new keyword.
I get an access violation exception when I step through this code and I see the callback map Root() is uninitialized. Since I am trying to populate the callback map only after constructing an instance of the configuration class, why is the static callback map still uninitialized?
What's "Root()"???
Below is the basic code structure:
-- In CConfig.h class CConfig { public: ... // Note that Register is not a static function bool Register(const std::string& skey, CallbackFn callback); private: // want to share this map across all instances static std::map<std::s tring, CallbackFn> s_callbacks; };
-- In CCMA.h class CCMA : public CSection { ... };
-- In CCMA.cpp // anonymous namespace namespace { // ReadCMA -> new CCMA and then read relevant tags from config file bool bRegisterCMA = G_CONFIG.Regist er("CMA_START" , ReadCMA); };
G_CONFIG returns either existing global pointer to CConfig after creating a new CConfig object if necessary simulating singleton behavior. However, we sometimes need more than one instance of CConfig for example to merge entries from 2 different configuration files and so we access these without using G_CONFIG (I still want to share the callback map).
Thanks
You're assuming that s_callbacks is initialized when the first instance of
the class is created. That's not the case. You need to initialize it
yourself somewhere. That can be done in the manner of a Singleton (inside
an accessor function), or at the global level (outside the class in your
implementation file).
-Howard
marvind wrote: Thank you for your response.
I wrapped the callback map as you suggested: stlCallbackMap& CallbackMap() { static stlCallbackMap callbackMap; return callbackMap; }
That "seems" to work. My mistake was that I thought that static data members of a class are initialized before the first instance of the class is constructed, instead of the runtime treating them as just globals in a namespace.
Are there good "free" tools that I can use to find out if there are such errors in the code since these errors depend on how the files are compiled?
If it depends on how it's compiled, then it's an error - whether it
shows or not.
As for tools: asserts, logs, traces. Other than that I don't know
free tools - though they might probably exist. In my company we
use MSVC7.1 which happens to have an excellent debugger, and for
real tough cases we use Bounds-Checker and/or VTune (profiling).
You might ask in a group/forum specific to whatever OS/compiler
you are using. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Qin Chen |
last post by:
I will present very long code, hope someone will read it all, and teach me
something like tom_usenet.
This question comes to me when i read <<Think in C++>> 2nd, chapter 10 ,
name control, section "Static initialization dependency". There is a
example to show how to solve the prolem involved with a technique first
poineered by Jerry Schwarz while creating the iostream library (because the
definitions for cin, cout, and cerr are static...
|
by: cppaddict |
last post by:
I have class with two static member objects, one of type int and one
of type vector<int>.
static int myStaticMemberInt
static vector<int> myStaticMemberVector;
I know how to initialize the int member:
MyClass::myStaticMemberInt = 99;
|
by: Per Bull Holmen |
last post by:
Hey
Im new to c++, so bear with me. I'm used to other OO languages, where it
is possible to have class-level initialization functions, that
initialize the CLASS rather than an instance of it. Like, for instance
the Objective-C method:
+(void)initialize
Which has the following characteristics: It is guaranteed to be run
|
by: Sandro Bosio |
last post by:
Hello everybody, my first message on this forum. I tried to solve my issue by reading other similar posts, but I didn't succeed. And forgive me if this mail is so long.
I'm trying to achieve the following (with incomplete succes): I want in a given namespace Parameters a list of "initializers" (which are objects derived from a simple interface that can be implemented anywhere, and are used to define which parameters the program will take at...
|
by: Pixel.to.life |
last post by:
Hi, Gurus,
I recently attempted to build a .Net forms application, that links
with old style unmanaged C++ static libs.
Of course I had to recompile the static lib projects to link properly
with the managed application.
My questions are two fold:
The managed project uses /clr and /MDd (in debug) options. The
| |
by: subramanian100in |
last post by:
Consider the following program:
#include <iostream>
using namespace std;
class Base
{
public:
Base(int x = 0);
|
by: akomiakov |
last post by:
Is there a technical reason why one can't initialize a cost static non-
integral data member in a class?
|
by: Jef Driesen |
last post by:
I have the following problem in a C project (but that also needs to
compile with a C++ compiler). I'm using a virtual function table, that
looks like this in the header file:
typedef struct device_t {
const device_backend_t *backend;
...
} device_t;
typedef struct device_backend_t {
|
by: Martin |
last post by:
I have an intranet-only site running in Windows XPPro, IIS 5.1, PHP
5.2.5. I have not used or changed this site for several months - the
last time I worked with it, all was well.
When I tried it just now, I am getting the subject error message
(specifically: PHP has encountered an access violation at 00F76E21).
The error is NOT occurring on every page request (but it is on most of
them) and, when I get the error, simply pressing <F5to...
|
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: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
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...
| |