0

I tried to add icon to my application window like this:

int width, height, channels;
unsigned char* pixels = stbi_load("resources/slika1.png", &width, &height, &channels, 4);

m_Icon[0].width = width;
m_Icon[0].height = height;
m_Icon[0].pixels = pixels;

glfwSetWindowIcon(m_Window, 1, m_Icon);

stbi_image_free(m_Icon[0].pixels);

And I get error from title.

I tried to google correct image size but had no luck. Also tried different sizes of png... still same error...

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • 1
    Check for any errors as stated in the docs: https://www.glfw.org/docs/3.3/group__window.html#gadd7ccd39fe7a7d1f0904666ae5932dc5 – kiner_shah Apr 03 '23 at 11:07

1 Answers1

0

stbi_load failed to load your image. pixels is null, and width, height and channels are uninitialized. Their contents are undefined.

The error is reported in windows.c, line 552, because the provided image dimensions contain a negative number, or zero.

m_Icon[0].width = width;
m_Icon[0].height = height;

You are initializing the image with data that was not initialized by STB; the data is read from the stack's memory, and this data is undefined in this context.

You need to do proper error handling:

bool loadIcon(void) {
    int width, height, channels;
    unsigned char* pixels = stbi_load("resources/slika1.png", &width, &height, &channels, 4);

    if (!pixels) {
        return false;
    }

    GLFWimage image;
    image.width = width;
    image.height = height;
    image.pixels = pixels;

    glfwSetWindowIcon(m_Window, 1, &image);

    stbi_image_free(pixels);
    return true;
}

Side note: you're assigning pixels to an outer variable, m_Icon, but then immediately deallocating this data with stbi_image_free(m_Icon[0].pixels). You can not use this data after freeing it.