473,883 Members | 1,788 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Singleton class fails on reboot


Hi,

I am writing a windows service. The code runs fine when I start the
service when my machine is running but
it fails to start automatically when the machine reboots. The code
bombs out when it reaches code that tries to access
a singleton class. This is the code.

void ClientLogging:: initClientError Log(Installatio nInfo install)
{

// next line fails on a reboot
string szLogLevel =
ConfigManager:: Instance().getC onfigMgrValue(" LOG_LEVEL");

Logging::Instan ce().Initialize ("IseeCltLog ");

Logging::LogLev el severity =
Logging::Instan ce().toSeverity (szLogLevel);

m_pFileLogAppen der = new FileLogAppender (severity, logFile); //this
line fails on reboot
if(false == Logging::Instan ce().addAppende r(m_pFileLogApp ender))
{

}

}

The strange thing is ClientLogging is also a singleton, the first one
that is called, and this works fine.
This is called using:

ClientLogging:: Instance().init ClientErrorLog( install);

But it is inside this that the problems arise, on the line as seen
above

string szLogLevel =
ConfigManager:: Instance().getC onfigMgrValue(" LOG_LEVEL");

The code just terminates.
Is there anything I can do to prevent this, any pragma.
Could this be because I initialize static variables.
Your help would be greatly appreciated.
Enda

Jul 5 '06
12 1904

ke************* ***@yahoo.co.uk wrote:
Hi,

Thanks for all your replies.

I do think it is a singleton issue.

When I comment out the code above the service starts correctly.
But that could be because both the lines you identify as failing
involve file access.

How about this: try commenting out the body of
ClientLogging:: initClientError Log() except for a call to
ConfigManager:: Instance(), and then comment out any file access code
inside the ctor for ConfigManager.

I'd wager that this will run fine, and both your singletons will be
created just as you expect. (Of course, without your ConfigManager
initialised properly, your program will probably fail to do what it's
supposed to - but it will show you if file access is the issue.)

The method you show of initialising a singleton is probably fine. It's
a lazy method (where no instance will be created if never required),
but in a multithreaded environment this is hard to do efficiently
without locking.

The method of using a static (although it is better if this is a static
class member, rather than a function local static) works (and is
efficient - requiring no locking), but is not lazy and is obviously
only possible if the resources required to create it are available at
runtime (when such globals are usually initialised). (This is also the
situation in which you can get yourself into dependency problems -
which you originally thought [and I guess, still think] you had.)

HTH,
Doug

Jul 6 '06 #11

mlimber wrote:
mlimber wrote:
See chapter 6 of _Modern C++ Design_ for more than you ever wanted to
know about templates in C++.

^^^^^^^^^

I meant "singletons " not "templates" (although, the latter might also
apply). --M

Thanks, I will try it out.

But this is the singleton template I was using, do you see any issues
why this would not start when the machine reboots.
class DLLEXPORT CSingleton
{
public:

// Return A reference to the instance of the CSingleton class.
// If there is no instance of the class yet, one will be created.
static T& Instance()
{
if (m_instance == 0 )
{
if (m_instance == 0)
{
m_instance = new T;

// exit processing function
std::atexit(CSi ngleton::Destro yInstance);
}
}
return *(CSingleton::m _instance);
};

// Destroys the CSingleton class instance.
// Be aware that all references to the single class instance will be
// invalid after this method has been executed!
static void DestroyInstance ()
{
delete m_instance;
m_instance = NULL;
};

protected:

// shield the constructor and destructor to prevent outside sources
// from creating or destroying a CCSingleton instance.

inline explicit CSingleton() { CSingleton::m_i nstance =
static_cast<T*> (this); }
inline ~CSingleton() { CSingleton::m_i nstance = 0; }

private:

inline explicit CSingleton(CSin gleton const&) {}
inline CSingleton& operator=(CSing leton const&) { return *this; }

private:

static T* m_instance; // CSingleton class instance
};

// static class member initialisation.
//template <typename TT* CSingleton<T>:: m_instance = 0;

~Enda

Jul 6 '06 #12

ke************* ***@yahoo.co.uk wrote:
mlimber wrote:
mlimber wrote:
See chapter 6 of _Modern C++ Design_ for more than you ever wanted to
know about templates in C++.
^^^^^^^^^

I meant "singletons " not "templates" (although, the latter might also
apply). --M


Thanks, I will try it out.

But this is the singleton template I was using, do you see any issues
why this would not start when the machine reboots.
class DLLEXPORT CSingleton
{
public:

// Return A reference to the instance of the CSingleton class.
// If there is no instance of the class yet, one will be created.
static T& Instance()
{
if (m_instance == 0 )
{
if (m_instance == 0)
{
m_instance = new T;

// exit processing function
std::atexit(CSi ngleton::Destro yInstance);
}
}
return *(CSingleton::m _instance);
};

// Destroys the CSingleton class instance.
// Be aware that all references to the single class instance will be
// invalid after this method has been executed!
static void DestroyInstance ()
{
delete m_instance;
m_instance = NULL;
};

protected:

// shield the constructor and destructor to prevent outside sources
// from creating or destroying a CCSingleton instance.

inline explicit CSingleton() { CSingleton::m_i nstance =
static_cast<T*> (this); }
inline ~CSingleton() { CSingleton::m_i nstance = 0; }

private:

inline explicit CSingleton(CSin gleton const&) {}
inline CSingleton& operator=(CSing leton const&) { return *this; }

private:

static T* m_instance; // CSingleton class instance
};

// static class member initialisation.
//template <typename TT* CSingleton<T>:: m_instance = 0;

~Enda
Hiya,

Other than the odd double-if test around mInstance (which won't help
with locking, if that's what it's for), I can't see any reason why this
would give you any problems.

Doug

Jul 9 '06 #13

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

Similar topics

7
12485
by: Tim Clacy | last post by:
Is there such a thing as a Singleton template that actually saves programming effort? Is it possible to actually use a template to make an arbitrary class a singleton without having to: a) explicitly make the arbitrary class's constructor and destructor private b) declare the Singleton a friend of the arbitrary class
7
3296
by: Ethan | last post by:
Hi, I have a class defined as a "Singleton" (Design Pattern). The codes are attached below. My questions are: 1. Does it has mem leak? If no, when did the destructor called? If yes, how can I avoid it? Purify does not show it has mem leak. // test if singleto class has mem leakage #include <iostream>
1
1550
by: Franz | last post by:
Hello NG I added a new tracelog class in a existing assebley witch is located in GAC. If I try to access a methode of this new class I'll get an errormeassage like this: Could not load type xxxx from assembly xxxx, Version=1.0.4850.0, Culture=neutral, PublicKeyToken=302009412214000a. I searched in ng and found some tasks, like 'do not use enums' or 'make sure you rebuild asembley before creating a new msi', 'check
2
1933
by: EvilOldGit | last post by:
In the stuff I've read about multi threaded singleton it seems to assume that creating the Lock is atomic, and various sources, including Sutter go for RAII. { Lock l; if ( ! inst ).... } But I don't see how to do this with a real O/S API. The ones I've looked at have a two step process to making a lock, they require a "Create" function and a "Start Locking" function.
3
18269
weaknessforcats
by: weaknessforcats | last post by:
Design Pattern: The Singleton Overview Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That is, regardless of where the object is hidden, everyone needs access to it. The global point of access is the object's Instance() method. Individual users need to be prevented from creating their own instances of the Singleton.
3
3298
by: gary.bernstein | last post by:
I want to call a singleton getInstance function to retrieve a templatized object without knowing what types were used to create the singleton object in the first call to getInstance. How can I do this non-intrusively -- I.e., without, for example, typedef'ing the types in every compilation unit? Background: Our code base has assert macros that need to reboot the system after notifying components via a single templatized Component...
9
3345
by: Tony Proctor | last post by:
I need to create a process Singleton object for an ASP application, but I'm having some odd issues In my GLOBAL.ASA, I have an <OBJECTelement specifying the relevant ProgID with RUNAT=Server and SCOPE=Application However, although this works fine for some classes, I get a permission denied error for classes from a particular DLL. The error response is:- Active Server Pages error '8002802b'
1
1717
by: =?Utf-8?B?QU1lcmNlcg==?= | last post by:
Sorry this is so long winded, but here goes. Following the model of http://msdn2.microsoft.com/en-us/library/system.runtime.remoting.channels.ipc.ipcchannel.aspx I made a remote object using the IpcChannel Class (vs 2005, vb, fw 2.0). Everyting works fine. The object is registered with WellKnownObjectMode.Singleton The remote object appears at the bottom of this posting. The code is deliberately obtuse to expose an issue about when...
4
2836
by: John Doe | last post by:
Hi, I have a singleton class defined like this : class UIManager : public CSingleton<UIManager>, public CObject { protected: DECLARE_DYNAMIC(UIManager) friend class CSingleton<UIManager>;
0
9792
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10848
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,...
0
9575
agi2029
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...
1
7972
isladogs
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...
0
7130
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();...
0
5798
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5992
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4614
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
3
3234
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.