Help? An implementation error? Methinks not.

Started by
3 comments, last by Bradsco 19 years, 9 months ago
But of course I could be wrong... okay I have a singleton class (sigh.. another singleton question) but hey, it works. Problem is that it doesn't work as I'd expect on both Win32 and OS X. The idea is as follows. A singleton error logging class. An Init function to open the log An Entry function that takes a string, formats it and writes the formatted log entry. The first line in this method is a check to make sure the FILE* is valid. A Shutdown function that closes things up. Everything works great in Win32. The logging class is one of a kind, universally accessible from anywhere necessary and writes out every entry sent to it. Here's the problem... under OS X (a Carbon application for those who might ask) only the class that first calls Init function (which opens the log file for writing) is able to successfully write entries into the log. Every other class or method that calls the Entry function dumps out because the FILE* is zero. I'm stumped.. any ideas?
I set the clouds in motion, turn up light and sound...Activate the window, and watch the world go 'round
Advertisement
Post the code of the class, it might shine some light on the problem. Which compilers did you use?

Toolmaker

Declaration

class ErrorLog{public:	static ErrorLog* Instance() { static ErrorLog instance; return &instance };	bool OpenLog(const char* filename);	bool LogEntry(const char* title, const char* message, ...);	bool CloseLog(void);protected:	ErrorLog() { };	~ErrorLog() { };private:	FILE* logFile;};


Implementation

bool ErrorLog::OpenLog(const char* filename){	if((logFile = fopen(filename, "w") == 0)		return false;	... do stuff	return true;};bool ErrorLog::LogEntry(const char* title, const char* message, ...){	if(logFile == 0)		return false;	... do stuff		return true;}bool ErrorLog::CloseLog(void){	fclose(logFile);	... do stuff	return true;}


In Windows I'm using Visual C++ .NET 2002 and in OS X I'm using XCode 1.1
I set the clouds in motion, turn up light and sound...Activate the window, and watch the world go 'round
I've actually run into a problem similar to this while porting code to linux from win32. It turned out that the error was how I was creating my singleton class (exactly how you are currently); the fix was to use a better singleton class like the following:

*******************

template <typename T> class ISingleton
{

public:

ISingleton(void) {

#if defined(_MSC_VER) && _MSC_VER < 1200
int iOffset = (int)(T*)1 - (int)(ISingleton <T>*)(T*)1;
ms_pSingleton = (T*)((int)this + iOffset);
#else
ms_pSingleton = static_cast< T* >(this);
#endif

}

virtual ~ISingleton(void) {
ms_pSingleton = 0;
}

static T& getInstance(void) {
return (*ms_pSingleton);
}

static T* getInstancePtr(void) {
return (ms_pSingleton);
}

protected:

static T* ms_pSingleton;

};

*******************

Then override getInstance() and getInstancePtr() in your child class like so:

*******************


template<> CLASSNAME *ISingleton<CLASSNAME>::ms_pSingleton = 0;

//************************************************************************

//-------------
// getInstance
//-------------
CLASSNAME &CLASSNAME::getInstance(void)
{

if(!ms_pSingleton)
new CLASSNAME();

return ISingleton<CLASSNAME>::getInstance();

}

//----------------
// getInstancePtr
//----------------
CLASSNAME*CLASSNAME::getInstancePtr(void)
{

if(!ms_pSingleton)
new CLASSNAME();

return ISingleton<CLASSNAME>::getInstancePtr();

}

*******************

Don't forget to 'delete this' in the destructor btw :)

Good luck!


-Nate S.
"Always forgive your enemies, nothing annoys them more.."
I know I shouldn't have thought any different, but man doesn't it just *feel* like a downer when supposedly portable code isn't?! Thank you very much Nate for the reply, I'm going to get on this singleton fix. If anybody else happens to read along this far, are there any thoughts on an easy to use CVS server that I can access from both the platforms I've mentioned? (Visual Studio .NET 200 and XCode 1.1)

EDIT: For those that might follow. The singleton handling is indeed an issue between platforms. Based on Nate's suggestion the code now interoperates between WindowsXP and OS X flawlessly.

[Edited by - Schmedly on June 28, 2004 12:56:26 PM]
I set the clouds in motion, turn up light and sound...Activate the window, and watch the world go 'round

This topic is closed to new replies.

Advertisement