473,695 Members | 1,560 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 #1
12 1885

ke************* ***@yahoo.co.uk wrote:
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
Your second flagged failure involves file access. I'm guessing your
first flagged failure also involves file access (maybe reading a config
file?)

I can only think of one thing - a security issue: the user your service
is starting as after a reboot is not the same user as you are when you
start it manually, and the former doesn't have access rights to the
required files?

But I'm probably wrong...

I think the singleton thing is a red herring.

Doug

Jul 5 '06 #2

<ke************ ****@yahoo.co.u kwrote in message
news:11******** *************@b 68g2000cwa.goog legroups.com...
>
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.
We can't tell from the little code you've shown. I'd suspect that the
problem has to do with how you're using or configuring the service (e.g.,
permissions, as Doug suggests). It could also have to do with the order of
initialization of static variables, but you haven't shown those variables or
how they're initialized.

I'd suggest you ask in a windows newsgroup with any windows-related
questions.

But if you think this is really a C++ _language_ question, you could try to
temporarily change the code so that your functions don't do anything
specific to Windows, and see if you still get the same behavior. Then post
the code (with the static variables involved and how they're initialized)
here and we can help see if it's a language-related issue.

-Howard

Jul 5 '06 #3
It could also be a service dependency thing. Your service could be dependent
on (say) the RPC service, but if you haven't installed it that way it will
come up first and fail. Of course those other dependent services are
already running when you start yours manually later.
--
Phil Wilson [MVP Windows Installer]
----
<ke************ ****@yahoo.co.u kwrote in message
news:11******** *************@b 68g2000cwa.goog legroups.com...
>
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 #4

ke************* ***@yahoo.co.uk wrote:
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.
Your singletons should be created inside a static member function as a
local static variable.
That's the only way to garantee, that your singleton will be created in
a just-in-time order.
If you create your singleton as a global variable or a static member
variable, there's no way to garantee it will be initialized when it's
first used by another global object (like another singleton).
Example:
foo& foo::Instance() //Where Instance is declared as a static method
{
static foo my_foo;
return my_foo;
}

Jul 6 '06 #5
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. The
service is already dependent on the RPC service. I have been through a
microsoft support case to verify this.

But I would like to hear more about the last reply from Axter.

This is my current Instance() function.

Foo& Foo::Instance(v oid)
{
if(_theInstance == 0)
_theInstance = new Foo();
return *_theInstance;
}

How do I change this to incorporate your last advice.
Enda
Axter wrote:
ke************* ***@yahoo.co.uk wrote:
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.

Your singletons should be created inside a static member function as a
local static variable.
That's the only way to garantee, that your singleton will be created in
a just-in-time order.
If you create your singleton as a global variable or a static member
variable, there's no way to garantee it will be initialized when it's
first used by another global object (like another singleton).
Example:
foo& foo::Instance() //Where Instance is declared as a static method
{
static foo my_foo;
return my_foo;
}
Jul 6 '06 #6
* ke************* ***@yahoo.co.uk:
[top-posting, over-quoting, cross-posting to Windows group]
Please don't top-post in [clc++]; please don't quote irrelevant
material; please don't cross-post post to platform-specific groups.

And please read the [clc++] FAQ before posting to [clc++].

The FAQ has a section about how to post.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 6 '06 #7

Alf P. Steinbach wrote:
* ke************* ***@yahoo.co.uk:
[top-posting, over-quoting, cross-posting to Windows group]

Please don't top-post in [clc++]; please don't quote irrelevant
material; please don't cross-post post to platform-specific groups.

And please read the [clc++] FAQ before posting to [clc++].

The FAQ has a section about how to post.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Ok,
Sorry about that,

but can someone explain to me how to implement this Just in time
singleton based on my current instance function.

Foo& Foo::Instance(v oid)
{
if(_theInstance == 0)
_theInstance = new Foo();
return *_theInstance;
}

Enda

Jul 6 '06 #8
ke************* ***@yahoo.co.uk wrote:
can someone explain to me how to implement this Just in time
singleton based on my current instance function.

Foo& Foo::Instance(v oid)
{
if(_theInstance == 0)
_theInstance = new Foo();
return *_theInstance;
}
Well, we can't see the rest of your code, but here's a more complete
example:

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;
}

Which is used as a wrapper like this:

class A
{
public:
void DoSomething();
// ...

private:
// Private constructor/destructor disallows creation
// except by friends.
friend class Singleton<A>;
A();
~A();

// Disabled functions for singleton usage
A( const A& );
A& operator=( const A& );
A* operator&();
};

typedef Singleton<AtheA ;

void Foo()
{
theA::Instance( ).DoSomething() ;
}

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

Cheers! --M

Jul 6 '06 #9
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

Jul 6 '06 #10

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

Similar topics

7
12473
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
3280
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
1540
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
1926
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
18242
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
3283
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
3324
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
1708
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
2824
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
8565
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,...
0
9113
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8839
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
7656
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...
0
5837
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
4339
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
4577
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2997
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
1971
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.