tags:

views:

138

answers:

1
+1  Q: 

Memory problems

Hello, I have some singleton class (please, don't speak about singleton usage).

class InputSystem : boost::serialization::singleton<InputSystem>
{
private:
   boost::shared_ptr<sf::Window> mInputWindow;
public:
   InputSystem()
   {
      mInputWindow = boost::shared_ptr<sf::Window>( new sf::Window(someARgs) );
      someMethod();
   }

   void someMethod()
   {
      mInputWindow->...() // Calling some methods of sf::Window class
      // Everything  is fine here
   }

   const sf::Input &Handle() const
   {
      return mInputWindow.get()->GetInput();
   }
};

void main()
{
   InputSystem::get_mutable_instance().Handle(); // Here is all members of InputSystem have invalid addresses in memory (0x000)
}

What's wrong could be there?

+3  A: 

Here is all members of InputSystem have invalid addresses in memory (0x000)

Either someMethod() is zeroing your class data, or you have misdiagnosed the issue.

Change your main function to this:

InputSystem& inputSystem = InputSystem::get_mutable_instance();
inputSystem.Handle();

This puts the creation of the singleton and the first attempt to use it onto separate lines. Fire up your debugger and step through the code looking for the exact point that your singleton's data is corrupted.

Gunslinger47
At the point of getting instance of the class, all data are destroyed.
Ockonal
@Ockonal: Is your constructor ever called?
Gunslinger47
@gunslinger47 yeah, debugger shows me that `someMethod()` calls.
Ockonal
Is `mInputWindow` null when `someMethod()` is called?
Gunslinger47
@gunslinger47 nope, everything is fine there
Ockonal
Is `mInputWindow` null at the end of `someMethod()`? X-D
Gunslinger47
@gunslinger47 :D Nope, it's right there.
Ockonal
@Ock: So when _does_ it turn null, then?
Gunslinger47
When calling `InputSystem::get_mutable_instance();` in main function.
Ockonal