OpenGL内存泄漏

时间:2015-01-11 21:30:46

标签: c++ opengl memory

我正在开始使用现代OpenGL。

但我面前是VBO和VAO的奇怪内存泄漏。 这是我的代码(重要部分):

#include "OpenGlScene.h"
#include "Block.h"
#include <iostream>
#include "Shader.h"
#include "Camera2D.h"
#include "Input.h"
#include "Container.h"
#include "Texture.h"

OpenGlScene::OpenGlScene(void) {
    this->width = 1280;
    this->height = 720;
    this->initWindow();
    this->initOpenGl();
    this->loop();
}


OpenGlScene::~OpenGlScene(void)
{
}

void OpenGlScene::loop() {

    bool mustFinish = false;
    unsigned int frameRate = ( 1000 / 60 );
    int startTime, endTime, elapsedTime = 0;

    Container* cContainer = new Container();

    Input *iInput = new Input();
    cContainer->setInput(iInput);
    iInput->setContainer(cContainer);

    Camera2D *cCamera = new Camera2D();
    cContainer->setCamera(cCamera);
    cCamera->setContainer(cContainer);
    cCamera->init(this->width, this->height);
    glm::mat4 cameraMatrix;


    Block *bBlock = new Block(0,0);
    bBlock->setContainer(cContainer);
    bBlock->init();

    Block *aBlock = new Block(-100, -100);
    aBlock->setContainer(cContainer);
    aBlock->init();


    Block *cBlock = new Block(-200, -100);
    cBlock->setContainer(cContainer);
    cBlock->init();

    //bBlock->load();

    while(!mustFinish) {

        startTime = SDL_GetTicks();

        cCamera->update();

        iInput->update();
        bBlock->update();


        cameraMatrix = cCamera->getCameraMatrix();
        bBlock->render();
        aBlock->render();
        cBlock->render();
        SDL_GL_SwapWindow(this->Window);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) ;
        glClearColor(119/255.0f,181/255.0f,254/255.0f,1);


        endTime = SDL_GetTicks();
        elapsedTime = endTime - startTime;

        if(elapsedTime < frameRate) {

            SDL_Delay(frameRate - elapsedTime);
        }
    }

}

bool OpenGlScene::initOpenGl() {


    GLenum glewInit( glewInit() );

    if(glewInit != GLEW_OK)
    {

        std::cout << "Erreur d'initialisation de GLEW : " << glewGetErrorString(glewInit) << std::endl;

        SDL_GL_DeleteContext(this->OpenGlContext);
        SDL_DestroyWindow(this->Window);
        SDL_Quit();

        return false;
    }
    glEnable(GL_DEPTH_TEST);

    return true;
}


bool OpenGlScene::initWindow() {


    if(SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        std::cout << "Erreur lors de l'initialisation de la SDL : " << SDL_GetError() << std::endl;
        SDL_Quit();

        return false;
    }

    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);

    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);

    this->Window = SDL_CreateWindow("OpenGL Scene", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, this->width, this->height, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);

    if(this->Window == 0) {
        std::cout << "Erreur lors de la creation de la fenetre : " << SDL_GetError() << std::endl;
        SDL_Quit(); 

        return false;
    }

    SDL_GLContext context = SDL_GL_CreateContext(this->Window);
    this->OpenGlContext = &context;

    if((*this->OpenGlContext) == 0) {
        std::cout << SDL_GetError() << std::endl;
        SDL_DestroyWindow(this->Window);
        SDL_Quit();

        return false;
    }


    return true;
}

漏洞来自内部循环方法。

它主要来自我渲染块。

#include "Block.h"


Block::Block(float x, float y){
    this->time = 0.0f;
    this->x = x;
    this->y = y;
    this->size = 200.0f;
    this->sSprite = nullptr;
}

Block::~Block(void){
    if(this->sSprite) {
        delete this->sSprite;
    }
}


void Block::init() {
    this->sSprite = new SpriteRect(x, y, size, size, "textures/grass.png", 1.0f, 1.0f, 1.0f, 1.0f);
    this->sSprite->setContainer(this->cContainer);
    this->sSprite->load();
}

void Block::update() {
    this->time += 0.5f;
    this->x += 0.5f;

    //this->sSprite->update(this->x, this->y);

}

void Block::render() {

    this->sSprite->render();

}

最重要的是,SpriteRect.cpp:

#include "SpriteRect.h"
#include <string>
#include "Texture.h"
#include "Shader.h"
#include "Container.h"
#include "Camera2D.h"

SpriteRect::SpriteRect(float x, float y, float width, float height, std::string textureFile, float r, float g, float b, float a) {
    this->x = x;
    this->y = y;
    this->width = width;
    this->height = height;
    this->vaoId = 0;
    this->vboId = 0;

    this->tTexture = new Texture(textureFile);
    this->tTexture->load();

    this->cColor = new Color();
    this->cColor->a = a;
    this->cColor->r = r;
    this->cColor->g = g;
    this->cColor->b = b;



    float textureCoordsTmp[] = {0.0f, 1,  1,1,   0.0f,0.0f,
                                0.0f,0.0f,    1,1,    1,0.0f};

    float colorCoordsTmp[] = {this->cColor->r,this->cColor->g,this->cColor->b,this->cColor->a,    this->cColor->r,this->cColor->g,this->cColor->b,this->cColor->a,   this->cColor->r,this->cColor->g,this->cColor->b,this->cColor->a, 
                                this->cColor->r,this->cColor->g,this->cColor->b,this->cColor->a,    this->cColor->r,this->cColor->g,this->cColor->b,this->cColor->a,     this->cColor->r,this->cColor->g,this->cColor->b,this->cColor->a};


    for(int i = 0; i<12; i++) {
        this->textureCoords[i] = textureCoordsTmp[i];
    }
    for(int i= 0; i<24;i++) {
        this->colors[i] = colorCoordsTmp[i];
    }

    this->setVerticesFromCoords();

    this->colorsSizeBytes = (24*sizeof(float));
    this->textureCoordsSizeBytes = (12*sizeof(float));
    this->verticesSizeBytes = (12*sizeof(float));


    this->sShader = new Shader("Shaders/classic2D.vert", "Shaders/classic2D.frag");
    this->sShader->charger();
}

SpriteRect::~SpriteRect() {

    delete this->tTexture;
    delete this->cColor;
    delete this->sShader;
    glDeleteBuffers(1, &vboId);
    glDeleteVertexArrays(1, &vaoId);
}

void SpriteRect::setVerticesFromCoords() {

    float verticesTmp[] = {x, y,  x+width, y,   x, y+height,
                            x, y+height, x+width, y,  x+width, y+height};   

    for(int i=0;i<12;i++) {
        this->vertices[i] = verticesTmp[i];
    }

    if(glIsBuffer(vboId) == GL_TRUE) {
        this->updateVbo(this->vertices, 12*sizeof(float), 0);
    }

}

void SpriteRect::load() {
    if(glIsBuffer(vboId) == GL_TRUE) {
        glDeleteBuffers(1, &vboId);
    }

    glGenBuffers(1, &vboId);
    glBindBuffer(GL_ARRAY_BUFFER, vboId);

    glBufferData(GL_ARRAY_BUFFER, (this->verticesSizeBytes + this->colorsSizeBytes + this->textureCoordsSizeBytes), 0, GL_STATIC_DRAW);

        glBufferSubData(GL_ARRAY_BUFFER, 0, this->verticesSizeBytes, this->vertices);
        glBufferSubData(GL_ARRAY_BUFFER, this->verticesSizeBytes, this->colorsSizeBytes, this->colors);
        glBufferSubData(GL_ARRAY_BUFFER, this->verticesSizeBytes+this->colorsSizeBytes, this->textureCoordsSizeBytes, this->textureCoords);

    glBindBuffer(GL_ARRAY_BUFFER, 0);

    if(glIsVertexArray(vaoId)) {
        glDeleteVertexArrays(1, &vaoId);
    }

    glGenVertexArrays(1, &vaoId);
    glBindVertexArray(vaoId);


        glBindBuffer(GL_ARRAY_BUFFER, vboId);


            glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
            glEnableVertexAttribArray(0);

            glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(this->verticesSizeBytes));
            glEnableVertexAttribArray(1);

            glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(this->verticesSizeBytes+this->colorsSizeBytes));
            glEnableVertexAttribArray(2);





        glBindBuffer(GL_ARRAY_BUFFER, 0);




    glBindVertexArray(0);
}

void SpriteRect::update(float x, float y) {

    if(x != this->x ||  y != this->y) {

        this->x = x;
        this->y = y;
        this->setVerticesFromCoords();

    }
    this->update();

}

void SpriteRect::update() {

}

void SpriteRect::updateVbo(void *datas, int sizeBytes, int offset)
{
    // Verrouillage du VBO

    glBindBuffer(GL_ARRAY_BUFFER,vboId);


        // Récupération de l'adresse du VBO

        void *vboAdress = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);


        // Si l'adresse retournée est nulle alors on arrête le transfert

        if(vboAdress == NULL)
        {
            std::cout << "Erreur au niveau de la récupération du VBO" << std::endl;
            glBindBuffer(GL_ARRAY_BUFFER, 0);

            return; 
        }


        // Mise à jour des données

        memcpy((char*)vboAdress + offset, datas, sizeBytes);


        // Annulation du pointeur

        glUnmapBuffer(GL_ARRAY_BUFFER);
        vboAdress = 0;


    // Déverrouillage du VBO

    glBindBuffer(GL_ARRAY_BUFFER, 0);
}

void SpriteRect::render() {


    glUseProgram(this->sShader->getProgramID());

        glBindVertexArray(vaoId);

        glUniformMatrix4fv(glGetUniformLocation(this->sShader->getProgramID(), "cameraMatrix"), 1, GL_FALSE, glm::value_ptr(this->cContainer->getCamera()->getCameraMatrix()));


            glBindTexture(GL_TEXTURE_2D, this->tTexture->getId());

            glDrawArrays(GL_TRIANGLES, 0, 6);

            glBindTexture(GL_TEXTURE_2D, 0);

        glBindVertexArray(0);

    glUseProgram(0);


}

然后当我注释掉[BlockObject] - &gt; render();没有更多的泄漏。 实际上泄漏大约是30Kb /秒。我看到当使用SpriteRect的updateVbo()方法时有泄漏。 抱歉,评论是法语,但代码很简单;)

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

对于您执行的每个new,必须有相应的delete。您的loop方法在本地变量中执行了大量new,但没有单delete