简体   繁体   English

OpenGL ES:FBO /纹理泄漏内存

[英]OpenGL ES: FBO / Texture Leaks Memory

I've worked with Apple's GLImageProcessing Example - Where I apply various filters to an image. 我使用过Apple的GLImageProcessing示例 - 我将各种过滤器应用于图像。 I've followed GLImageProcessing Multiple Filters? 我跟着GLImageProcessing Multiple Filters? to make it work with two filters. 使其适用于两个过滤器。

The Original example by Apple works great at doesn't leak memory. Apple的原创示例非常出色,不会泄漏内存。

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. 使用上面提到的问题中的代码使应用程序在10-20秒的使用时消耗所有iPhone内存,给我一个内存警告,最后使应用程序崩溃。

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) 我知道它与创建aditional纹理和帧缓冲对象(FBO)有关,但我不确定如何正确管理它们(及其内存)

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? 有一个更好的方法吗?

What you're doing looks fine, but you're never deleting the textures or the FBOs. 你正在做什么看起来很好,但你永远不会删除纹理或FBO。 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. 否则,他们会永远地徘徊。

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: 因此,尝试将以下行移动到示例代码的initGL方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM