【问题标题】:Resource Leak opengl/win32资源泄露 opengl/win32
【发布时间】:2016-09-16 23:13:55
【问题描述】:

您好,我最近开始学习 win32/opengl,并设法编写了一个在窗口中显示多色立方体的函数。我的问题是存在资源泄漏,但我很困惑,不确定我到底忘记删除什么。

注意我已经把它缩小到这个函数内

void display() 
{
    g.hglrc = wglCreateContext(g.hdc);
    wglMakeCurrent(g.hdc, g.hglrc);

    // make the color a white hue  
    glClearColor(1.0F, 1.0F, 1.0F, 1.0F);

    //  Clear screen and Z-buffer
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Reset transformations
    glLoadIdentity();

    // Rotate when user changes rotate_x and rotate_y
    glRotatef(rotate_x, 1.0, 0.0, 0.0);
    glRotatef(rotate_y, 0.0, 1.0, 0.0);

    //Multi-colored side - FRONT
    glBegin(GL_POLYGON);
    glColor3f(1.0, 0.0, 0.0);     glVertex3f(0.5, -0.5, -0.5);      // P1 is red
    glColor3f(0.0, 1.0, 0.0);     glVertex3f(0.5, 0.5, -0.5);      // P2 is green
    glColor3f(0.0, 0.0, 1.0);     glVertex3f(-0.5, 0.5, -0.5);      // P3 is blue
    glColor3f(1.0, 0.0, 1.0);     glVertex3f(-0.5, -0.5, -0.5);      // P4 is purple
    glEnd();

    // White side - BACK
    glBegin(GL_POLYGON);
    glColor3f(1.0, 1.0, 1.0);
    glVertex3f(0.5, -0.5, 0.5);
    glVertex3f(0.5, 0.5, 0.5);
    glVertex3f(-0.5, 0.5, 0.5);
    glVertex3f(-0.5, -0.5, 0.5);
    glEnd();

    // Purple side - RIGHT
    glBegin(GL_POLYGON);
    glColor3f(1.0, 0.0, 1.0);
    glVertex3f(0.5, -0.5, -0.5);
    glVertex3f(0.5, 0.5, -0.5);
    glVertex3f(0.5, 0.5, 0.5);
    glVertex3f(0.5, -0.5, 0.5);
    glEnd();

    // Green side - LEFT
    glBegin(GL_POLYGON);
    glColor3f(0.0, 1.0, 0.0);
    glVertex3f(-0.5, -0.5, 0.5);
    glVertex3f(-0.5, 0.5, 0.5);
    glVertex3f(-0.5, 0.5, -0.5);
    glVertex3f(-0.5, -0.5, -0.5);
    glEnd();

    // Blue side - TOP
    glBegin(GL_POLYGON);
    glColor3f(0.0, 0.0, 1.0);
    glVertex3f(0.5, 0.5, 0.5);
    glVertex3f(0.5, 0.5, -0.5);
    glVertex3f(-0.5, 0.5, -0.5);
    glVertex3f(-0.5, 0.5, 0.5);
    glEnd();

    // Red side - BOTTOM
    glBegin(GL_POLYGON);
    glColor3f(1.0, 0.0, 0.0);
    glVertex3f(0.5, -0.5, -0.5);
    glVertex3f(0.5, -0.5, 0.5);
    glVertex3f(-0.5, -0.5, 0.5);
    glVertex3f(-0.5, -0.5, -0.5);
    glEnd();

    wglMakeCurrent(NULL, NULL);

    SwapBuffers(g.hdc);
    ReleaseDC(g.hwnd, g.hdc);
    wglDeleteContext(g.hglrc);
}

【问题讨论】:

  • 是什么让你认为存在“资源泄漏”?
  • 如果我让程序与显示调用一起运行,内存使用量很容易上升到 100 mb 并继续增长。如果我删除对这个函数的调用,无论它运行多长时间,使用量都会保持在 27-28mbs 左右。所以我认为这是资源泄漏的结果,因为我没有看到任何其他会导致这种行为的东西。
  • 注释掉函数的内容,验证问题是否消失,然后逐渐取消注释一行/部分,直到问题再次出现。
  • 请停止更改问题。
  • 当您怀疑有泄漏时,更好的工作方法是使用名为 Valgrind 的工具。它将缩小到特定的呼叫。将该工具的输出粘贴到 SO 问题可能会得到更快的答案。

标签: c++ winapi opengl


【解决方案1】:
g.hglrc = wglCreateContext(g.hdc);

不要那样做。

您不必在每次需要重绘屏幕时都创建渲染上下文。你创建它一次;只有当你的 window 消失时它才会消失。

现在,这并不一定能证明为什么创建和销毁渲染上下文会留下资源。但这无关紧要;由于性能,你不应该这样做。渲染上下文创建和销毁不是一个快速的过程,也不是这样。

【讨论】:

  • 你说得对,当我将它从函数中取出时,性能会得到显着提升,但资源泄漏仍然存在。
  • 不只是您指出的那一行,而且显示循环中的所有 wgl 调用,尤其是靠近底部的其他 win32 行,它们使当前上下文为空并释放 DC 无疑会导致一些资源颠簸。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-23
  • 2019-02-14
  • 1970-01-01
  • 2010-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多