Q: OpenGL glDeleteTextures leading to memory leak? My program renders large jpegs as textures one at a time and allows the user to switch between them by re-using the "same" texture.
I call glGenTextures(1, &texture); once at the start.
Then each time I want to swap the image I use:
FreeTexture( texture );
ROI_img = fetch_image(temp, sortVector[tPiece]);

loadTexture_Ipl( ROI_img , &texture );

here are the two functions being called:
int loadTexture_Ipl(IplImage *image, GLuint *text)
{
    if (image==NULL) return -1;
    glBindTexture( GL_TEXTURE_2D, *text );

    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 4);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, ROI_WIDTH, ROI_HEIGHT,0, GL_BGR, GL_UNSIGNED_BYTE, image->imageData);
    return 0;
}

void FreeTexture(GLuint texture)
{
    glDeleteTextures( 1, &texture);
}

My problem is that after a couple of images they stop rendering (texture is all black). If I keep trying to switch I get this error message:
test(55248,0xacdf22c0) malloc: *** mmap(size=744001536) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
Mar 19 11:57:51 Ants-MacBook-Pro.local test[55248] <Error>: CGSImageDataLock: Cannot allocate memory

I thought that glDeleteTextures would free the memory each time??
Any ideas on how to better implement this? 
p.s. here is a screen of the memory leaks being encountered (https://p.twimg.com/AoWVy8FCMAEK8q7.png:large)

A: Since you want to reuse your texture, you shouldn't delete it with glDeleteTextures. If you do that, you need to create a new texture using glGenTextures, and I don't see you are doing it in the loadTexture_Ipl function.

A: glGenTextures and glDeleteTextures go in pairs. If you really want to delete your texture (maybe because you most likely won't be using that texture again), delete it like you already do and call glGenerateTextures again before setting a new texture 
