[C++][OpenGL] How to fix the Visual Studio 2022 Error code: 0xC0000005: Access violation reading location 0x0000000000000000.

loldope19 0 Reputation points
2023-07-27T02:16:27.4366667+00:00

I'm trying to rewrite my C++-OpenGL code into separate classes but I somehow keep getting an error code in the debugger on Visual Studio 2022 everytime I run my application:

"Exception thrown at 0x00007FFC3004FD97 (ig11icd64.dll) in glfwsetup.exe: 0xC0000005: Access violation reading location"

glfwsetup is my main coding file, but the exception is caught on another class 'Model3D'.

// Model3D.cpp

void Model3D::loadTexture() {
	std::string strTexPath = texturePath;
	stbi_set_flip_vertically_on_load(true);

	unsigned char* tex_bytes =
		stbi_load(texturePath,
			&img_width,
			&img_height,
			&colorChannels,
			0);

	if (tex_bytes == nullptr) {
		// Error loading texture image
		std::cerr << "Failed to load texture image: " << texturePath << std::endl;
		return;
	}
	// Generate a reference
	glGenTextures(1, &texture);
	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, texture);

	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img_width, img_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex_bytes);	// This is where the error occurs

	glGenerateMipmap(GL_TEXTURE_2D);	// Although Visual Studio flags it here
	stbi_image_free(tex_bytes);
}

I'm calling the loadTexture() function twice on my main file, but somehow the error occurs on the second function call.

// Main/glfwsetup.cpp    

/* * * * * * * * * */
	GLuint shaderProgram = glCreateProgram();
    glAttachShader(shaderProgram, vertShader);
    glAttachShader(shaderProgram, fragShader);

    glLinkProgram(shaderProgram);
    
    cSteve.loadModel();		// Loads Correctly
    cFire.loadModel();		// Exception Thrown

    modelVectors.push_back(cSteve);
    modelVectors.push_back(cFire);

I'll also throw in the .h file of Model3D in case I simply forgot something there:

// Model3D.h

#ifndef MODEL3D_H
#define MODEL3D_H

#include <glad/glad.h>
#include <GLFW/glfw3.h>

#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>

#include <iostream>
#include <vector>

namespace model {
	class Model3D {
		public:
			std::string path;
			const char* texturePath;
			float x, y, z;
			float scale_x, scale_y, scale_z;
			float axis_x, axis_y, axis_z;
			float rotateX, rotateY, rotateZ;
			int img_width, img_height, colorChannels;
			GLuint texture;

		private:
			GLuint VAO, VBO, EBO;
			glm::mat4 transformation_matrix;
			std::vector<GLuint> mesh_indices;
			std::vector<GLfloat> fullVertexData;

		public: 
			Model3D(std::string path, const char* texturePath, glm::vec3 modelPos);
			
		public:
			glm::mat4 transform();
			void loadModel();
			void loadTexture();
			void drawModel();
			void deleteBuffers();
			void setXAxis(float rotateX);
			void setYAxis(float rotateY);
			void setZAxis(float rotateZ);

		public:
			GLuint getVAO() const { return VAO; }
			const std::vector<GLuint>& getMeshIndices() const { return mesh_indices; }
			
	};
}

#endif

If you need more context, then I could send other code snippets if needed.

Any help would be appreciated!

Developer technologies | Visual Studio | Debugging
Developer technologies | C++
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.