Reputation: 855
I'm working with a 2D game that has multiple levels that can be loaded and (and should be unloaded.)
To make it easier to handle textures I wrote a TextureLoader class that has a list with all the textures, when an object in the game wants to use a texture it only has the path to the texture it wishes to use and then asks the TextureLoader if it can obtain a pointer to the texture with the same path. Then if the texture doesn't exist in the list the TextureLoader will attempt to load it before sending back the pointer.
This works fine throughout the game and when I reload a level the memory usage stays the same.
But when I load a new level I obviously want the previous level to be unloaded, however it doesn't seem to work.
That's where glDeleteTextures come in.
So what I try to do is:
int arraySize = textures.size();
GLuint* arr = new GLuint[arraySize];
int x = 0;
for (std::list<Texture2D*>::iterator it = textures.begin(); it != textures.end(); ++it)
{
arr[x] = (*it)->getImage();
x++;
}
glDeleteTextures(arraySize, arr);
textures.clear();
Since the pointers to the memory are stored in a seperate Texture2D class I attempt to collect all of them before calling glDeleteTextures;
However my memory usage keeps growing for every level until I reach a stack overflow.
getImage()
returns a GLuint with a pointer to the texture that I obtain when I first bound the texture.
What am I doing wrong?
Upvotes: 0
Views: 2259
Reputation: 855
After monitoring my VRam usage I can see that the Vram is being freed properly, so the issue is because OpenGL stores copies in the system RAM that don't get freed up when I call glDeleteTextures().
Upvotes: 0
Reputation: 4812
You are not incrementing 'x'.
Additionally you leak the memory of the array. Why not use a vector?
std::vector<GLuint> arr;
arr.reserve(textures.size());
for (std::list<Texture2D*>::iterator it = textures.begin(); it != textures.end(); ++it)
{
arr.push_back((*it)->getImage());
// Are you missing:
// delete *it;
}
if (!arr.empty()) {
glDeleteTextures(arr.size(), &arr[0]);
}
textures.clear();
Upvotes: 1