2

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

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.

With opengl context it takes 11MB.

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

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

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...

Here is the main.

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;
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
jack
  • 107
  • 1
  • 1
  • 5

1 Answers1

2

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

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.

Defter
  • 214
  • 1
  • 7
  • Well thank you, as always i was looking for the problem in the wrong place..... The 8 kb leak which stops after a while.... i suppose thats normal... made me go crazy and look for the problem elsewhere than it was. Thank you very much. Stays steady at 16MB now :) – jack Apr 16 '15 at 16:27
  • Also not need calling every iteration glBufferData and some others function if you reuse one VBO and vertices data not changed – Defter Apr 16 '15 at 16:29
  • This was only a test code if everything works... These function calls will go into classes then it will be optimized and changed :) but thank you, you have saved me a whole day looking for the problem in the wrong place. – jack Apr 16 '15 at 16:33