Exception while dealing with textures

I am following Victor Gordan’s tutorial on OpenGL. Everything has been going smoothly until I’ve encountered a strange error to which I couldn’t find a solution anywhere. The IDE is VS Community 2022 and I’m using it’s built-in debugger. The debugger is flagging an exception on Line 37 ‘glGenerateMipmap(GL_TEXTURE_2D);’ and it’s contents are: Exception thrown at 0x00007FF891080BDB (nvoglv64.dll) in OpenGL.exe: 0xC0000005: Access violation reading location 0x0000021123175000. Full Texture Class code:

#include"Texture.h"

Texture::Texture(const char* image, GLenum texType, GLenum slot, GLenum format, GLenum pixelType)
{
	// Assigns the type of the texture ot the texture object
	type = texType;

	// Stores the width, height, and the number of color channels of the image
	int widthImg, heightImg, numColCh;
	// Flips the image so it appears right side up
	stbi_set_flip_vertically_on_load(true);
	// Reads the image from a file and stores it in bytes
	unsigned char* bytes = stbi_load(image, &widthImg, &heightImg, &numColCh, 0);

	if (!bytes) std::cout << "wtf";
	// Generates an OpenGL texture object
	glGenTextures(1, &ID);
	// Assigns the texture to a Texture Unit
	glActiveTexture(slot);
	glBindTexture(texType, ID);

	// Configures the type of algorithm that is used to make the image smaller or bigger
	glTexParameteri(texType, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
	glTexParameteri(texType, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

	// Configures the way the texture repeats (if it does at all)
	glTexParameteri(texType, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(texType, GL_TEXTURE_WRAP_T, GL_REPEAT);

	// Extra lines in case you choose to use GL_CLAMP_TO_BORDER
	// float flatColor[] = {1.0f, 1.0f, 1.0f, 1.0f};
	// glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, flatColor);

	// Assigns the image to the OpenGL Texture object
	glTexImage2D(texType, 0, GL_RGBA, widthImg, heightImg, 0, format, pixelType, bytes);
	// Generates MipMaps
	glGenerateMipmap(GL_TEXTURE_2D);

	// Deletes the image data as it is already in the OpenGL Texture object
	stbi_image_free(bytes);

	// Unbinds the OpenGL Texture object so that it can't accidentally be modified
	glBindTexture(texType, 0);
}

void Texture::texUnit(Shader& shader, const char* uniform, GLuint unit)
{
	// Gets the location of the uniform
	GLuint texUni = glGetUniformLocation(shader.ID, uniform);
	// Shader needs to be activated before changing the value of a uniform
	shader.Activate();
	// Sets the value of the uniform
	glUniform1i(texUni, unit);
}

void Texture::Bind()
{
	glBindTexture(type, ID);
}

void Texture::Unbind()
{
	glBindTexture(type, 0);
}

void Texture::Delete()
{
	glDeleteTextures(1, &ID);
}

The format paramater in glTexImage2D describes the format of the pixel data you want to upload. Make sure it has the correct value and matches the number of channels in the image.

I passed GL_RGBA as the format parameter. The file extension is .png.

Then you need to be sure numColCh == 4. The last argument in stbi_load can be used to force a specific number of channels.
Besides there is also GL_UNPACK_ALIGNMENT, but I dont think thats the issue here.

I changed the line from unsigned char* bytes = stbi_load(image, &widthImg, &heightImg, &numColCh, 0); to unsigned char* bytes = stbi_load(image, &widthImg, &heightImg, &numColCh, 4); and it worked. Thanks for help, although I do not like to hard-code stuff as it might cause issues in the future, is there any way to adress the issue without hard-coding the numColCh value?

Nice. If you dont want to force it to 4 channels, you can write a function that returns the corresponding OpenGL pixel format for every number of channels you want to support.