I think I’m attempting the wrong approach here so please tell me if what I’m trying to achieve won’t work with this “technique”.
I have an object callled Spawner which spawns KillerObjects every X ms, when I want the KillerObject to be destroyed I call:
void Spawner::RemoveKillerObject(KillerObject* removeThis)
{
garbageList.push_back(removeThis);
}
Then everytime the Spawner is updated it will loop through the garbageList to remove every object that has been pushed into the garbage list, like this:
void Spawner::Update(float elapsedTime)
{
elapsedSince += elapsedTime;
if (elapsedSince > spawnRate)
{
Spawn();
elapsedSince = 0;
}
for each (KillerObject* removeThis in garbageList)
{
spawnedObjects.remove(removeThis);
}
garbageList.clear();
for each (KillerObject* ko in spawnedObjects)
{
ko->Update(elapsedTime);
}
}
Spawn is simply creating a pointer to a KillerObject and pushes it into the spawnedObject list.
The objects are “deleted” in the game as in I can’t collide with them and they won’t be drawn.
But this causes my memory usage to continuously grow, I know this is the cause because if I remove this code the memory usage is stable.
A point in the right direction or any other help is very much appreciated.
Kind regards
Markus
I must admit, that I have not completely understood your code, but you probably have a memory leak, i.e. produce more objects
new KillerObject()than youdelete. You could use a similar statement somewhere to remove the objects that are no longer needed:or just change: