2/2/2023
I’m working on a OpenGL project, and valgrind is detecting a memory leak from GLFW. I’m not sure what I’m doing wrong, but it occurs when I call glfwInit()
from what I can tell. I’m calling glfwTerminate()
at the end of the main function, so I don’t know why this is leaking.
(I’m using quite almost the “example” code for an OpenGL project.)
Valgrind log:
==784== Memcheck, a memory error detector
==784== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==784== Using Valgrind-3.19.0 and LibVEX; rerun with -h for copyright info
==784== Command: ./bin/OpenGL
==784==
==784==
==784== HEAP SUMMARY:
==784== in use at exit: 12,336 bytes in 3 blocks
==784== total heap usage: 46 allocs, 43 frees, 25,926 bytes allocated
==784==
==784== 4,096 bytes in 1 blocks are definitely lost in loss record 1 of 3
==784== at 0x4847A73: calloc (vg_replace_malloc.c:1328)
==784== by 0x52569A8: ???
==784== by 0x5268834: ???
==784== by 0x1239FD: _glfwConnectX11 (in /home/username/Documents/Source/OpenGL/bin/OpenGL)
==784== by 0x11A72B: _glfwSelectPlatform (in /home/username/Documents/Source/OpenGL/bin/OpenGL)
==784== by 0x114298: glfwInit (in /home/username/Documents/Source/OpenGL/bin/OpenGL)
==784== by 0x10E459: main (main.c:18)
==784==
==784== 8,240 (64 direct, 8,176 indirect) bytes in 1 blocks are definitely lost in loss record 3 of 3
==784== at 0x4842888: malloc (vg_replace_malloc.c:381)
==784== by 0x5256B56: ???
==784== by 0x5268834: ???
==784== by 0x1239FD: _glfwConnectX11 (in /home/username/Documents/Source/OpenGL/bin/OpenGL)
==784== by 0x11A72B: _glfwSelectPlatform (in /home/username/Documents/Source/OpenGL/bin/OpenGL)
==784== by 0x114298: glfwInit (in /home/username/Documents/Source/OpenGL/bin/OpenGL)
==784== by 0x10E459: main (main.c:18)
==784==
==784== LEAK SUMMARY:
==784== definitely lost: 4,160 bytes in 2 blocks
==784== indirectly lost: 8,176 bytes in 1 blocks
==784== possibly lost: 0 bytes in 0 blocks
==784== still reachable: 0 bytes in 0 blocks
==784== suppressed: 0 bytes in 0 blocks
==784==
==784== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
Example Code I’m using:
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <stdio.h>
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow *window);
// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
int main()
{
// glfw: initialize and configure
// ------------------------------
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// glfw window creation
// --------------------
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "OpenGL Window", NULL, NULL);
if (window == NULL)
{
printf("Failed to create GLFW window\n");
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
// glad: load all OpenGL function pointers
// ---------------------------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
printf("Failed to initialize GLAD\n");
return -1;
}
// uncomment this call to draw in wireframe polygons.
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
// render loop
// -----------
while (!glfwWindowShouldClose(window))
{
// input
// -----
processInput(window);
// rendering
glClearColor(0.3f, 0.2f, 0.3f, 0.5f);
glClear(GL_COLOR_BUFFER_BIT);
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
glfwSwapBuffers(window);
glfwPollEvents();
}
// glfw: terminate, clearing all previously allocated GLFW resources.
// ------------------------------------------------------------------
glfwTerminate();
return 0;
}
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
// ---------------------------------------------------------------------------------------------------------
void processInput(GLFWwindow* window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, 1);
}
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
// make sure the viewport matches the new window dimensions; note that width and
// height will be significantly larger than specified on retina displays.
glViewport(0, 0, width, height);
}
Thanks.