This is a pretty huge program, so I tried to narrow it down to what I think is causing the violation at 0xFEEEFEEE. Although a huge program, (by number of lines anyways) it does a pretty simple thing: Draw a triangle using OpenGL. One thing to note is that the program seems to only trigger an access violation if I x out the console, assuming because it doesn't destruct properly.
Header:
#include <iostream>
#include <vector>
#include <string>
class Mesh
{
public:
Mesh(std::string FileName);
~Mesh();
void Draw();
private:
unsigned int VBO = 0;
unsigned int VAO = 0;
std::vector<float> Vertices;
const char* VertexShader =
"#version 330\n"
"in vec3 vp;"
"void main () {"
" gl_Position = vec4 (vp, 2.0);"
"}";
const char* FragmentShader =
"#version 330\n"
"out vec4 frag_colour;"
"void main () {"
" frag_colour = vec4 (0.5, 0.0, 0.5, 1.0);"
"}";
};
#endif // MESH_H
cpp:
#define GLEW_STATIC
#include <GL\glew.h>
#include <glm\glm.hpp>
#include <iostream>
#include <fstream>
#include <string>
#include "Mesh.h"
Mesh::Mesh(std::string FileName)
{
float X;
float Y;
float Z;
int LoopCount = 1;
int VerticesCount = 0;
std::string Input;
std::ifstream OBJFile;
OBJFile.open(FileName);
while (!OBJFile.eof())
{
OBJFile >> Input;
if (Input == "v")
{
OBJFile >> X;
OBJFile >> Y;
OBJFile >> Z;
std::cout << "Loaded in " << X << " for X # " << LoopCount << std::endl;
std::cout << "Loaded in " << Y << " for Y # " << LoopCount << std::endl;
std::cout << "Loaded in " << Z << " for Z # " << LoopCount << std::endl;
Vertices.push_back(X);
Vertices.push_back(Y);
Vertices.push_back(Z);
LoopCount++;
VerticesCount = VerticesCount + 3;
}
else
{
std::cout << "In loading " << FileName << " " << "skipped " << Input << std::endl;
continue;
}
}
}
Mesh::~Mesh()
{
std::cout << "Deconstructed" << std::endl;
}
void Mesh::Draw()
{
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, Vertices.size() * sizeof(float), &Vertices.front(), GL_STATIC_DRAW);
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs, 1, &VertexShader, NULL);
glCompileShader(vs);
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fs, 1, &FragmentShader, NULL);
glCompileShader(fs);
GLuint shader_programme = glCreateProgram();
glAttachShader(shader_programme, fs);
glAttachShader(shader_programme, vs);
glLinkProgram(shader_programme);
glUseProgram(shader_programme);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
I then do Mesh myMesh
and then in my main loop Mesh.draw()