1

so im getting a memory leak from this constructor

TEXTURE::TEXTURE(std::string path) {
    // load and create a texture
    // -------------------------

    glGenTextures(1, &ID);
    glBindTexture(GL_TEXTURE_2D, ID); // all upcoming GL_TEXTURE_2D operations now have effect on this texture object
    // set the texture wrapping parameters
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);// set texture wrapping to GL_REPEAT (default wrapping method)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    // set texture filtering parameters
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    // load image, create texture and generate mipmaps
    int width, height, nrChannels;
    
    stbi_set_flip_vertically_on_load(1);
    unsigned char* data = stbi_load(path.c_str(), &width, &height, &nrChannels, 0);
    if (data)
    {
        if (nrChannels == 3) {
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
            glGenerateMipmap(GL_TEXTURE_2D);
        }
        if (nrChannels == 4) {
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
            glGenerateMipmap(GL_TEXTURE_2D);
        }
    }
    else
    {
        std::cout << "Failed to load texture " << path << std::endl;
    }
    stbi_image_free(data);
    
}

did some heap profiling using visual studio and the heap gets filled with void
here is the allocation call stack

OPENGL_PROJECT.exe!stbi__malloc() - Line 923
OPENGL_PROJECT.exe!stbi__malloc_mad3() - Line 988
OPENGL_PROJECT.exe!stbi__create_png_image_raw() - Line 4320
OPENGL_PROJECT.exe!stbi__create_png_image() - Line 4531
OPENGL_PROJECT.exe!stbi__parse_png_file() - Line 4846
OPENGL_PROJECT.exe!stbi__do_png() - Line 4895
OPENGL_PROJECT.exe!stbi__png_load() - Line 4926
OPENGL_PROJECT.exe!stbi__load_main() - Line 1042
OPENGL_PROJECT.exe!stbi__load_and_postprocess_8bit() - Line 1111
OPENGL_PROJECT.exe!stbi_load_from_file() - Line 1236
OPENGL_PROJECT.exe!stbi_load() - Line 1226
OPENGL_PROJECT.exe!TEXTURE::TEXTURE() - Line 20
OPENGL_PROJECT.exe!TextField::render() - Line 155
OPENGL_PROJECT.exe!main() - Line 176
   ...

so why isnt stbi_image_free not deallocating all memory? should i make a deconstructor for the class , or why is it leaking???

0 Answers0