473,395 Members | 2,006 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,395 software developers and data experts.

static initialization access violation error

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::string, 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.Register("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

Jul 23 '05 #1
4 2508
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::string, 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.Register("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_callbacks" is initialized before
"bRegisterCMA" is. One way to do so would be to grab a proper
Sincleton implementation, and make "s_callbacks" 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_callbacks" 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".
Jul 23 '05 #2
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?

Jul 23 '05 #3

"marvind" <ma********@yahoo.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.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::string, 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.Register("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

Jul 23 '05 #4
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.
Jul 23 '05 #5

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

Similar topics

1
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,...
15
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...
8
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....
1
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...
3
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...
2
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; class Base { public: Base(int x = 0);
15
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?
11
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...
39
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
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...
0
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...
0
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...

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.