473,583 Members | 3,089 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Error when initializing static variables

Hi,

I have some problems using static variables which results in a core
dump. I have attached code and coredump to the end of my message.

I am trying to implement a kind of factory design. I have a base class
with several sub classes. In runtime I want to create a instance of a
sub class and assign it to a base class pointer. Nothing fancy about
that. I also want to be able in runtime to decide witch type of sub
class that is to be instantiated. Another requirement is that I want to
be able to add new sub classes without changing the existing code base.
To accomplish this I have made two classes that make up the
factory-part of the design, Maker and SpecializedMake r. Maker contains
a std::map with a key and a pointer to Maker. Then specializedMake r
contains a static variable. All this is made with templates.

The makerMap is defined first and then every sub class is able to
define the static variable registerThis and by doing so adding a key
and a factory method in makerMap. It may sound complicated, but is
quite straight forward when looking at the code.

I have tried to shorten the attached example, but it is still quite
big. Sorry for the quite long post.

I know that instantiation of static variables can be a source of
trouble, but I could not find any other good way to do this.

Using CC 5.3 on Solaris gives me a working application, but when
upgrading to CC 5.7 my applications leaves a core dump instead.

My questions are:
1) Is there a better way to do this?
2) Ip my code meet the standard (and should work)? Or was I lucky when
it ran compiled with CC 5.3?
3) I the order of initialisations of static members standardized (i.e.
is the order fixed in one translation unit)?
program terminated by signal SEGV (no mapping at the fault address)
0x00013d2c: insert+0x002c: ld [%l0 + 4], %l0
(dbx) where

=>[1]
__rwstd::__rb_t ree<std::basic_ string<char,std ::char_traits<c har>,std::alloc ator<char>
,std::pair<con st std::basic_stri ng<char,std::ch ar_traits<char> ,std::allocator <char> >,Maker<A,int>* >,__rwstd::__se lect1st<std::pa ir<const std::basic_stri ng<char,std::ch ar_traits<char> ,std::allocator <char> >,Maker<A,int>* >,std::basic_st ring<char,std:: char_traits<cha r>,std::allocat or<char> > >,std::less<std ::basic_string< char,std::char_ traits<char>,st d::allocator<ch ar> > >,std::allocato r<std::pair<con st std::basic_stri ng<char,std::ch ar_traits<char> ,std::allocator <char> >,Maker<A,int>* > > >::insert(0xffb ef6f8, 0x27188, 0xffbef690, 0x1, 0xff06b8c8, 0x46ad0), at 0x13d2c

[2] SpecializedMake r<A,B,int>::Spe cializedMaker(0 x2716c, 0xffbef7e8,
0xffbef7e7, 0xff3de79c, 0x222b0, 0x1), at 0x13350
[3] __SLIP.INIT_A(0 x0, 0xff1421a0, 0xff13e5d8, 0xff1ea8d4, 0x222b0,
0xff09bc20), at 0x1310c
[4] __STATIC_CONSTR UCTOR(0x0, 0xff13c008, 0x16b48, 0xffffffff,
0x222b0, 0x16ba4), at 0x1329c
[5] 0x16c04(0x26fac , 0xff13c008, 0x16c18, 0x0, 0x0, 0x0), at 0x16c03

------------------------------------------------------------------------------

The code looks like this:

#include <iostream>
#include <string>
#include <map>

//
// Test classes
//

class A {

public:
A() : m_val(4711) {}
int getVal() {return m_val;}
private:
int m_val;
};

class B : public A {
public:
B(int i) : m_val2(i) {}
int getVal2() { return m_val2;}
private:
int m_val2;
};
//
// Maker
//
template<class T, class U = int> class Maker
{
public:

static T* newObject(const std::string& key, U arg)
{
Maker<T, U> * factory = 0;
factory = (*m_makerMap.fi nd(key)).second ;
if (factory != 0) {
return factory->createObject(a rg);
}
else {
return 0;
}
}

protected:

Maker (const std::string& key)
{
m_makerMap[const_cast<std: :string&>(key)] = this;
}

virtual T* createObject(U arg) const = 0;

private:

Maker() {}
Maker(const Maker& right) {}
Maker& operator= (const Maker& right) {}

static std::map<std::s tring, Maker<T,U>* > m_makerMap;

};
//
// SpecializedMake r
//
template<class T, class P, class U = int> class SpecializedMake r :
public Maker <T, U>
{

public:

T* createObject (U arg) const
{
return new P(arg);
}

private:

SpecializedMake r(const std::string& key)
: Maker <T, U>(key) {}

static const SpecializedMake r<T, P, U> m_registerThis;

};

template <class T, class U>
std::map<std::s tring, Maker<T, U>*> Maker<T, U>::m_makerMap ;

const SpecializedMake r<A, B>
SpecializedMake r<A, B>::m_registerT his("B");
//
// Main
//
int main(int argc, char** argv)
{
return 0;
}

Dec 15 '05 #1
3 5698
Diebels wrote:
Hi,

I have some problems using static variables which results in a core
dump. I have attached code and coredump to the end of my message.

I am trying to implement a kind of factory design. I have a base class
with several sub classes. In runtime I want to create a instance of a
sub class and assign it to a base class pointer. Nothing fancy about
that. I also want to be able in runtime to decide witch type of sub
class that is to be instantiated. Another requirement is that I want to
be able to add new sub classes without changing the existing code base.
To accomplish this I have made two classes that make up the
factory-part of the design, Maker and SpecializedMake r. Maker contains
a std::map with a key and a pointer to Maker. Then specializedMake r
contains a static variable. All this is made with templates.

The makerMap is defined first and then every sub class is able to
define the static variable registerThis and by doing so adding a key
and a factory method in makerMap. It may sound complicated, but is
quite straight forward when looking at the code.

I have tried to shorten the attached example, but it is still quite
big. Sorry for the quite long post.

I know that instantiation of static variables can be a source of
trouble, but I could not find any other good way to do this.

Using CC 5.3 on Solaris gives me a working application, but when
upgrading to CC 5.7 my applications leaves a core dump instead.

My questions are:
1) Is there a better way to do this?
2) Ip my code meet the standard (and should work)? Or was I lucky when
it ran compiled with CC 5.3?
3) I the order of initialisations of static members standardized (i.e.
is the order fixed in one translation unit)?
program terminated by signal SEGV (no mapping at the fault address)
0x00013d2c: insert+0x002c: ld [%l0 + 4], %l0
(dbx) where

=>[1]
__rwstd::__rb_t ree<std::basic_ string<char,std ::char_traits<c har>,std::alloc ator<char>
,std::pair<con st std::basic_stri ng<char,std::ch ar_traits<char> ,std::allocator <char> >,Maker<A,int>* >,__rwstd::__se lect1st<std::pa ir<const std::basic_stri ng<char,std::ch ar_traits<char> ,std::allocator <char> >,Maker<A,int>* >,std::basic_st ring<char,std:: char_traits<cha r>,std::allocat or<char> > >,std::less<std ::basic_string< char,std::char_ traits<char>,st d::allocator<ch ar> > >,std::allocato r<std::pair<con st std::basic_stri ng<char,std::ch ar_traits<char> ,std::allocator <char> >,Maker<A,int>* > > >::insert(0xffb ef6f8, 0x27188, 0xffbef690, 0x1, 0xff06b8c8, 0x46ad0), at 0x13d2c

[2] SpecializedMake r<A,B,int>::Spe cializedMaker(0 x2716c, 0xffbef7e8,
0xffbef7e7, 0xff3de79c, 0x222b0, 0x1), at 0x13350
[3] __SLIP.INIT_A(0 x0, 0xff1421a0, 0xff13e5d8, 0xff1ea8d4, 0x222b0,
0xff09bc20), at 0x1310c
[4] __STATIC_CONSTR UCTOR(0x0, 0xff13c008, 0x16b48, 0xffffffff,
0x222b0, 0x16ba4), at 0x1329c
[5] 0x16c04(0x26fac , 0xff13c008, 0x16c18, 0x0, 0x0, 0x0), at 0x16c03

------------------------------------------------------------------------------

The code looks like this:

#include <iostream>
#include <string>
#include <map>

//
// Test classes
//

class A {

public:
A() : m_val(4711) {}
int getVal() {return m_val;}
private:
int m_val;
};

class B : public A {
public:
B(int i) : m_val2(i) {}
int getVal2() { return m_val2;}
private:
int m_val2;
};
//
// Maker
//
template<class T, class U = int> class Maker
{
public:

static T* newObject(const std::string& key, U arg)
{
Maker<T, U> * factory = 0;
factory = (*m_makerMap.fi nd(key)).second ;
if (factory != 0) {
return factory->createObject(a rg);
}
else {
return 0;
}
}

protected:

Maker (const std::string& key)
{
m_makerMap[const_cast<std: :string&>(key)] = this;
}

virtual T* createObject(U arg) const = 0;

private:

Maker() {}
Maker(const Maker& right) {}
Maker& operator= (const Maker& right) {}

static std::map<std::s tring, Maker<T,U>* > m_makerMap;

};
//
// SpecializedMake r
//
template<class T, class P, class U = int> class SpecializedMake r :
public Maker <T, U>
{

public:

T* createObject (U arg) const
{
return new P(arg);
}

private:

SpecializedMake r(const std::string& key)
: Maker <T, U>(key) {}

static const SpecializedMake r<T, P, U> m_registerThis;

};

template <class T, class U>
std::map<std::s tring, Maker<T, U>*> Maker<T, U>::m_makerMap ;

const SpecializedMake r<A, B>
SpecializedMake r<A, B>::m_registerT his("B");
//
// Main
//
int main(int argc, char** argv)
{
return 0;
}


Interestingly, I had a very similar issue recently. See this thread,
and particularly the posts from Nov 21 and after:

http://groups.google.com/group/comp....e6e05865ffa786

The similarity is especially acute since you are using a static
variable to register with the factory. See the cited thread for a
standard conformant solution to that problem.

One other thing I would suggest is getting rid of the static map in
Maker<> and making the class a singleton instead. This would ensure
that no "static initialization order fiasco" is in play. See this FAQ
and following:

http://www.parashift.com/c++-faq-lit...html#faq-10.12

Something like the "Meyers singleton" might be good enough (if not see
_Modern C++ Design_, ch. 6 for other options):

template<class T>
class Singleton
{
public:
static T& Instance();
private:
// Disabled functions
Singleton();
Singleton( const Singleton& );
Singleton& operator=( const Singleton& );
Singleton* operator&();
~Singleton();
};

template<class T>
T& Singleton<T>::I nstance()
{
static T myObject;
return myObject;
}

typedef Singleton< SpecializedMake r<A,B> > theMaker;

void Foo()
{
std::auto_ptr<A > a( theMaker::Insta nce().createObj ect( 42 ) );
// ...
}

Cheers! --M

Dec 15 '05 #2

mlimber skrev:
One other thing I would suggest is getting rid of the static map in
Maker<> and making the class a singleton instead. This would ensure
that no "static initialization order fiasco" is in play. See this FAQ
and following:


This is exactly what I am doing right now. It seems to work, but I have
to convert some of the new code to templates and do some more testing
before I am sure this is the way to go.

Thanks!

//daniel

Dec 15 '05 #3
"Diebels" <da************ @gmail.com> wrote in message
news:11******** *************@g 47g2000cwa.goog legroups.com
Hi,

I have some problems using static variables which results in a core
dump. I have attached code and coredump to the end of my message.
[snip] The code looks like this:

#include <iostream>
#include <string>
#include <map>

//
// Test classes
//

class A {

public:
A() : m_val(4711) {}
int getVal() {return m_val;}
private:
int m_val;
};

class B : public A {
public:
B(int i) : m_val2(i) {}
int getVal2() { return m_val2;}
private:
int m_val2;
};
//
// Maker
//
template<class T, class U = int> class Maker
{
public:

static T* newObject(const std::string& key, U arg)
{
Maker<T, U> * factory = 0;
factory = (*m_makerMap.fi nd(key)).second ;
if (factory != 0) {
return factory->createObject(a rg);
}
else {
return 0;
}
}

protected:

Maker (const std::string& key)
{
m_makerMap[const_cast<std: :string&>(key)] = this;
}

virtual T* createObject(U arg) const = 0;

private:

Maker() {}
Maker(const Maker& right) {}
Maker& operator= (const Maker& right) {}

static std::map<std::s tring, Maker<T,U>* > m_makerMap;

};
//
// SpecializedMake r
//
template<class T, class P, class U = int> class SpecializedMake r :
public Maker <T, U>
{

public:

T* createObject (U arg) const
{
return new P(arg);
}

private:

SpecializedMake r(const std::string& key)
: Maker <T, U>(key) {}

static const SpecializedMake r<T, P, U> m_registerThis;

};

template <class T, class U>
std::map<std::s tring, Maker<T, U>*> Maker<T, U>::m_makerMap ;
Adding

template <>
std::map<std::s tring, Maker<A>*> Maker<A>::m_mak erMap;

here seems to solve the problem (though I don't fully understand what is
going on).

The omission of template<> from the next line is non-standard
const SpecializedMake r<A, B>
SpecializedMake r<A, B>::m_registerT his("B");
//
// Main
//
int main(int argc, char** argv)
{
return 0;
}

--
John Carson
Dec 15 '05 #4

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

Similar topics

1
3663
by: James | last post by:
Hello Java NG, I not sure if this is the right NG for this type of question but if not please let me know which is, TIA Any way first off let me say I'm a student and this WAS last weeks lab, turned in, graded and passed so I'm not trying to get someone to do my lab assignments, but after I got this back I was reading about the...
17
5610
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm constantly running tests on imperfect code. On of the cumbersome jobs encountered is reassigning global vars their values after a close encounter with...
21
4390
by: Anthony England | last post by:
Everyone knows that global variables get re-set in an mdb when an un-handled error is encountered, but it seems that this also happens when the variable is defined as private at form-level. So if "global variables get re-set" doesn't tell the whole story, then what does? ***please note*** I'm not looking for a solution - I'm looking for a...
1
417
by: Andreas Boehm | last post by:
Hi *.*, does the standard meanwhile define something about initializing variables by the compiler? I think, it is a side-effect of the OS used, if undefined global (static) variables are initialized to zero. What does the standard define for this? thanx Andreas
0
1107
by: Adam Smith | last post by:
Are there any drawbacks to initializing static member variables for classes used in an asp.net application within application_start? My classes have arraylist like tables, which cache information so that I don't have to hit the database every call. I would like to initialize these tables through calling a static class function, Init(), in...
10
7852
by: sunil | last post by:
Hello, I am new to c# . I have some basic programming doubts. Please help me in clarifying these doubts. I want to initialize a static and readonly field with a value returned by a static method. How ever, when I am debugging, that method is not being called. So I am not able to figure out, whether the field is getting initialized properly...
0
1216
by: Adamn | last post by:
Hi everybody! I'm using Visual Studio 2005 and don't know how to solve this problem. Till now, everything worked just fine, included all the necessary libs, headers and so on. But there is the problem: I have to implement a class ColorSpaces with static member functions, which manipulates a buffer from the class MSImage and returns it. ...
5
1845
by: iu2 | last post by:
Hi all, This code compiles: int func1() { return 3; } void func2()
2
13182
by: Frostmur | last post by:
Hi !! I'm trying to convert C code to C++. This is my function: static void (*selection)(void) = NULL; static void (*pick)(GLint name) = NULL; void zprSelectionFunc(void (*f)(void))
0
7895
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7826
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8182
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. ...
0
8193
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...
0
5374
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...
0
3843
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2333
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
1
1433
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1157
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...

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.