stage = new Intro(/*parameters*/);
Then, inside the intro stage, I press ESCAPE which pauses, and then "Exit to Menu", which exits to the menu and frees the memory.
delete stage;
stage = NULL;
The problem is that the memory is not getting freed!!! (I am checking the memory consumption from task manager)
Intro.h
#ifndef STAGE_INTRO_H
#define STAGE_INTRO_H
#include <hgesprite.h>
#include <hgeresource.h>
#include <hge.h>
#include <hgeparticle.h>
#include <cstdint>
#include <memory>
#include "Stage.h"
#include "hgePoint.h"
class Intro : public Stage
{
public:
Intro(hgeResourceManager *_resourceManager);
~Intro();
void Update(float dt);
void Render();
void HandleInput();
const char *StageName() const { return "Intro"; }
private:
Intro(const Intro &intro);
Intro &operator = (const Intro &intro);
HGE *hge;
hgeSprite *background, *ship;
hgePoint shipPos;
float shipScale;
hgeResourceManager *resourceManager;
static const uint16_t STARS = 500;
hgeParticleSystem *stars[STARS];
hgePoint starPositions[STARS];
static const float RESPAWN_TIME;
float timePassed[STARS]; // time passed since respawned -> respawns every 2 seconds
};
#endif
Intro.cpp
#include "Stage.h"
#include "Intro.h"
const float Intro::RESPAWN_TIME = 2.0f;
// constructor
Intro::Intro(hgeResourceManager *_resourceManager) : hge(hgeCreate(HGE_VERSION)), resourceManager(_resourceManager), shipScale(1.0f)
{
resourceManager->Precache(1);
background = resourceManager->GetSprite("intro_background");
ship = resourceManager->GetSprite("spaceship");
shipPos.Set(-ship->GetWidth() * 1.1f, hge->System_GetState(HGE_SCREENHEIGHT)*0.5f - ship->GetHeight() * 0.5f);
if( !background ) { hge->System_Log("Intro::Intro -> background could not be loaded"); exit(-1); }
if( !ship ) { hge->System_Log("Intro::Intro -> ship could not be loaded"); exit(-1); }
for(auto i = 0; i < STARS; ++i)
{
try
{
stars = new hgeParticleSystem("data/particles/very_small_star.psi", resourceManager->GetSprite("star_particle"));
}
catch(...)
{
hge->System_Log("In Intro::Intro : Memory for hgeParticleSystem (%d of %d) could not be allocated", (int)i, (int)STARS);
exit(-1);
}
starPositions.Set((float)hge->Random_Int(0.0f, hge->System_GetState(HGE_SCREENWIDTH) - 1), (float)hge->Random_Int(0, hge->System_GetState(HGE_SCREENHEIGHT) - 1));
stars->FireAt(starPositions.x, starPositions.y);
timePassed = hge->Random_Float(0, RESPAWN_TIME);
}
}
// destructor
Intro::~Intro()
{
for(auto i = 0; i < STARS; ++i)
delete stars;
resourceManager->Purge(1);
hge->Release();
}
void Intro::Update(float dt)
{
shipPos.x += 25 * dt;
shipScale -= 0.0005f;
for(auto i = 0; i < STARS; ++i)
{
stars->Update(dt);
timePassed += dt;
if( timePassed > RESPAWN_TIME )
{
timePassed = 0.0f;
starPositions.Set((float)hge->Random_Int(0.0f, hge->System_GetState(HGE_SCREENWIDTH) - 1), (float)hge->Random_Int(0, hge->System_GetState(HGE_SCREENHEIGHT) - 1));
stars->MoveTo(starPositions.x, starPositions.y);
}
}
}
void Intro::Render()
{
background->Render(0, 0);
ship->RenderEx(shipPos.x, shipPos.y, 0, shipScale, shipScale);
for(auto i = 0; i < STARS; ++i)
stars->Render();
}
void Intro::HandleInput()
{
}
Any ideas?