glewInit() 根据valgrind导致内存泄漏

问题描述

我正在尝试调试和修复程序中的所有内存泄漏。我已经浏览了整个源代码,没有一个 newmalloc() 调用不受 free()delete 支持。我尝试在 valgrind 中运行该程序。 Valgrind 发现一致的(在程序的多次执行过程中)844 字节的数据肯定丢失了。它还不断地将我指向我的 Window 类的 glewInit() 函数。我做错了什么吗?

需要注意的几点:

  • 我的窗口类是完全静态的
  • 我的窗口类调用 InputManager::init(),它也是一个静态类
  • 我有一个用于存储常量的完全静态类
class Window {
public:

     // void create(unsigned int width,unsigned int height,const std::string& name,bool resizable,bool decorated){
    //
    // }

    static void create(unsigned int width,bool decorated){

         if(!glfwInit()){
              Utils::log("Failed to initialize GLFW");
            return;
         }


         //Setting Window settings
         glfwWindowHint(GLFW_RED_BITS,8);
         glfwWindowHint(GLFW_GREEN_BITS,8);
         glfwWindowHint(GLFW_BLUE_BITS,8);
         glfwWindowHint(GLFW_ALPHA_BITS,8);
         glfwWindowHint(GLFW_DEPTH_BITS,24);
         glfwWindowHint(GLFW_STENCIL_BITS,8);
         glfwWindowHint(GLFW_DOUBLEBUFFER,GLFW_TRUE);
         glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,3);
         glfwWindowHint(GLFW_CONTEXT_VERSION_MInor,3);
         glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE);
         glfwWindowHint(GLFW_SAMPLES,4);
         glfwWindowHint(GLFW_RESIZABLE,resizable ? GLFW_TRUE : GLFW_FALSE);
         glfwWindowHint(GLFW_DECORATED,decorated ? GLFW_TRUE : GLFW_FALSE);

         m_width = width;
         m_height = height;

    #ifdef __APPLE__
         glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT,GL_TRUE);
    #endif

         //Creating the window
         window = glfwCreateWindow(width,height,name.c_str(),NULL,NULL);
         if(!window){
              Utils::log("Window: Failed to create window");
            return;
         }

         //Settings for window
         glfwSwapInterval(1);
         glfwSetFramebufferSizeCallback(window,windowResized);

         //Creating the context for opengl
         glfwMakeContextCurrent(window);

         //Initializing glew
         if(glewInit() != GLEW_OK){
              Utils::log("Window: Failed to initialize glew");
         }

         //Enabling transparency
         glEnable(GL_BLEND);
         glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);

         //Enabling depth
         glEnable(GL_DEPTH_TEST);
         glClearDepthf(1.0f);

         //Enabling back face culling
         glEnable(GL_CULL_FACE);
         glCullFace(GL_BACK);

         //Enabling MSAA
         glEnable(GL_MULTISAMPLE);

         InputManager::init(window);

    }
     static void clear();
     static void update();
     static void close();

     //Window functions
     static void setVerticalSyncEnabled(bool enabled);
     static void setMouseCursorGrabbed(bool grabbed);
     static int getWidth();
     static int getHeight();
     static bool isResized();

     static bool isCloseRequested();

     static GLFWwindow* window;


private:

     static void windowResized(GLFWwindow* window,int width,int height);

     static int m_width;
     static int m_height;

     static bool m_isResized;
     static bool m_closeRequested;


};
#endif

解决方法

我开始使用 GLAD,它不再导致内存泄漏。