I'm loading a texture using a class that I've written, as shown below. The aim is to create a new texture, load it with data that is stored in an array, and render it onto the screen. The texture is created without any problem, and loaded as well. However, it's causing memory leaks where the application memory size keeps increasing, despite calling delete in order to invoke the Texture class' destructor to delete the textures.
void renderCurrentGrid(){
// Get the current grid (custom wrapper class, it's working fine)
Array2D<real_t> currentGrid = m_Continuum->getCurrentGrid();
// load it as a texture
Texture *currentGridTexture = new Texture(GL_TEXTURE_2D);
currentGridTexture->load(currentGrid.sizeWd(), currentGrid.sizeHt(), ¤tGrid(0,0));
......
delete currentGridTexture;
}
Texture class' load function:
bool Texture::load(const size_t width, const size_t height, const float *data)
{
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// Allocate a texture name
glGenTextures(1, &m_TextureObj);
// select current texture
glBindTexture(GL_TEXTURE_2D, m_TextureObj);
// select replace to ensure texture retains each texel's colour
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
// if texture wraps over at the edges
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glGenerateMipmap(GL_TEXTURE_2D);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE,/* &density(0,0)*/data);
m_Loaded = true;
return true;
}
and the destructor of the Texture class:
Texture::~Texture()
{
glBindTexture(m_TextureTarget, m_TextureObj);
glDeleteTextures(1, &m_TextureObj);
glBindTexture(m_TextureTarget, NULL);
}
Texture class' private data members:
private:
GLenum m_TextureTarget;
GLuint m_TextureObj;
bool m_Loaded;
Here, I've created the new texture, and loaded it using the texture class' load function. The textures render absolutely fine, but the loading causes memory leaks. The application memory size keeps increasing when I check it on task manager.
However, when I comment out the line
currentGridTexture->load(....);
the memory leaks stop. I've omitted the rendering functions,a sit's not relevant to the question.