Hey all.
Been working on my game recently using a state manager that Exploiter developed.
I have made a physics manager class in which I am going to add all my Box2D stuff.
Header
#pragma once
#include <Box2D/Box2D.h>
class PhysicsManager
{
public:
PhysicsManager(void);
~PhysicsManager(void);
private:
b2World *mWorld;
};
cpp
#include "PhysicsManager.h"
#include <iostream>
PhysicsManager::PhysicsManager(void)
{
b2Vec2 Gravity(0.f, 9.4f);
mWorld = new b2World(Gravity, false);
}
PhysicsManager::~PhysicsManager(void)
{
std::cout << "Deleting Physics Manager" << std::endl;
delete mWorld;
}
as you can see, very simple - all it does is make a new mWorld at the moment and delete it in its destructor.
Now, say I allocate this in my 'Play' state for instance -
class PlayState : public GameState
{
public:
PlayState( GameEngine& game, bool replace = true );
void pause();
void resume();
void update();
void draw();
private:
PhysicsManager mPhysicsManager;
};
now, this will allocate the mPhysicsManager on the stack, so when PlayState is killed when the app ends, mPhysicsManager will go out off scope so its destructor should be called correct? Thus deleting mWorld in my Physics Manager class.
but this doesn't happen. I get a horrible memory leak coming from mWorld..
if I explicitly call the destructor in my playstate constructor (so it deletes it straight after its created) it cleans up fine.. just not when playstate goes out of scope.