Search code examples
c++openglmemoryglew

glewInit() Causes memory leak according to valgrind


I am trying to debug and fix all memory leaks in my program. I have went through the entire source code and there is not one call of new or malloc() which isn't supported by a free() or delete. I tried running the program in valgrind. Valgrind found that a consistent(throughout multiple executions of the program) 844 bytes of data were definitely lost. It also constantly points me back to the glewInit() function of my Window class. Is there anything I am doing wrong?

Couple things to note:

  • My window class is completely static
  • My window class calls InputManager::init() which is also a static class
  • I have another completely static class for storing constants
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, unsigned int height, const std::string& name, bool resizable, 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


Solution

  • I started using GLAD and it stopped causing the memory leak.