c ++ Opengl,Win内存泄漏 [英] c++ Opengl, Win memory leak

查看:120
本文介绍了c ++ Opengl,Win内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 WIN api 在 Windows 7 上创建了一个简单的 opengl 程序.

I've created a simple opengl program on Windows 7 using WIN api.

我按照 MSDN win api 网站上的教程设置了窗口.该窗口运行良好,即使有输入日志记录,它也不会泄漏任何内存并且运行良好,内存为 1.7 MB.

I've setup the window by following the tutorial on MSDN win api website. The window works perfectly, even with input logging it does not leak any memory and works alright stays at 1.7 MB of ram.

使用 opengl 上下文需要 11MB.

With opengl context it takes 11MB.

如果我想绘制矩形之类的东西,它会开始泄漏 200 kb.(矩形画得很好....)

If i want to draw something like Rectangle it begins to leak by 200 kb. (The rectangle draws perfectly fine....)

它从 14.5 MB 开始,增加到 50MB 并继续.

It starts at 14.5 MB and grows up to 50MB and continues.

整个程序没有'new'关键字,非常简单的程序.只创建窗口.比完成渲染的 while 循环...

There are no 'new' keywords in the whole program, it is very simple program. Only create window. Than while loop in which the rendering is done...

这里是主要的.

int main()
{

    Window wind(800,600);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    while(!wind.isCloseRequested())
    {
       glClear(GL_COLOR_BUFFER_BIT);
       glLoadIdentity();

       Vector3f Vertices[3];
       Vertices[0] = Vector3f(-1.0f, -1.0f, 0.0f);
       Vertices[1] = Vector3f(1.0f, -1.0f, 0.0f);
       Vertices[2] = Vector3f(0.0f, 1.0f, 0.0f);

       GLuint VBO;
       glGenBuffers(1, &VBO);
       glBindBuffer(GL_ARRAY_BUFFER, VBO);
       glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);

       glEnableVertexAttribArray(0);
       glBindBuffer(GL_ARRAY_BUFFER, VBO);
       glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

       glDrawArrays(GL_TRIANGLES, 0, 3);

       glDisableVertexAttribArray(0);
       wind.update();
       Sleep(16);
   }
   wind.Destroy();

   return 0;
}

推荐答案

glGenBuffers(1, &VBO);造成内存泄漏.您需要创建缓冲区一次并使用它或每次使用 glDeleteBuffers 销毁缓冲区

glGenBuffers(1, &VBO); make memory leak. You need create buffer one time and using it or destroy buffer every time with glDeleteBuffers

顺便说一下,在使用任何 OpenGL 函数之前,我推荐阅读文档.许多OpenGL函数在内存中分配一些内部缓冲区或GPU中的资源,需要调用delete/destroy.

By the way, before using any OpenGL function I recommended read docs. Many OpenGL function allocate some internal buffers in memory or resources in GPU and need call delete/destroy.

这篇关于c ++ Opengl,Win内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆