Segfaults with singletons

Posted by Ockonal on Stack Overflow See other posts from Stack Overflow or by Ockonal
Published on 2010-06-10T14:20:22Z Indexed on 2010/06/10 14:42 UTC
Read the original article Hit count: 152

Filed under:
|
|

Hello, I have such code:

// Non singleton
class MyLogManager
{
  void write(message) {Ogre::LogManager::getSingletonPtr()->logMessage(message);}
}

class Utils : public singleton<Utils>
{
   MyLogManager *handle;
   MyLogManager& getHandle { return *handle; }
};

namespace someNamespace
{
   MyLogManager &Log() { return Utils::get_mutable_instance().getHandle(); }
}

int main()
{
   someNamespace::Log().write("Starting game initializating...");
}

In this code I'm using boost's singleton (from serialization) and calling Ogre's log manager (it's singleton-type too).

The program fails at any trying to do something with Ogre::LogManager::getSingletonPtr() object with code

User program stopped by signal (SIGSEGV)

I checked that getSingletonPtr() returns address 0x000

But using code Utils::get_mutable_instance().getHandle().write("foo") works good in another part of program. What's wrong could be there with calling singletons?

© Stack Overflow or respective owner

Related posts about c++

Related posts about crash