Jakob Halskov
Jakob Halskov

Reputation: 438

OpenGL ES: FBO / Texture Leaks Memory

I've worked with Apple's GLImageProcessing Example - Where I apply various filters to an image. I've followed GLImageProcessing Multiple Filters? to make it work with two filters.

The Original example by Apple works great at doesn't leak memory.

Using the code from the Question mentioned above makes the app consume all of the iPhone memory in 10-20 seconds of use giving me a Memory Warning and finally making the app crash.

I know it's related to creating the aditional Textures and Frame Buffer Objects (FBO's) but I'm not sure how to correctly manage them (and their memory)

The code:

void drawGL(int wide, int high, float contrastVal,float brightnessVal)
{
    //INIT
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrthof(0, wide, 0, high, -1, 1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glScalef(wide, high, 1);


    glGetIntegerv(GL_FRAMEBUFFER_BINDING_OES, (GLint *)&SystemFBO);


    // Create the texture and the FBO the will hold the result of applying the first filter
    glGenTextures(1, &ResultTexture);
    glBindTexture(GL_TEXTURE_2D, ResultTexture);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, wide, high, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
    glGenFramebuffersOES(1, &ResultTextureFBO);
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, ResultTextureFBO);
    glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, ResultTexture, 0);


    glBindFramebufferOES(GL_FRAMEBUFFER_OES, ResultTextureFBO);
    glBindTexture(GL_TEXTURE_2D, Input.texID);

    glViewport(0, 0, wide, high);
    brightness(flipquad, contrastVal);
    glCheckError();;


    glBindFramebufferOES(GL_FRAMEBUFFER_OES, SystemFBO);
    glBindTexture(GL_TEXTURE_2D, ResultTexture);

    glViewport(0, 0, wide, high);
    contrast(fullquad,brightnessVal);
    glCheckError(); 
}

Is there a better way to do this?

Upvotes: 0

Views: 1475

Answers (2)

Andy Barnard
Andy Barnard

Reputation: 706

I had the exact same problem. The memory warning seems to be a result of generating the textures each time the method is called.

Therefore, try moving the following lines to the initGL method of your example code:

glGenTextures(1, &ResultTexture);
glGenFramebuffersOES(1, &ResultTextureFBO);

Upvotes: 1

user1118321
user1118321

Reputation: 26395

What you're doing looks fine, but you're never deleting the textures or the FBOs. You need to call:

glDeleteTextures (1, ResultTexture);
glDeleteBuffers (1, ResultTextureFBO);

at some point to free up the memory they use. Otherwise, they'll hang around forever.

Upvotes: 1

Related Questions