Has stopped working

Started by
10 comments, last by alx119 6 years, 10 months ago

Hi,

When I run the OpenGL application, it appears "Project.exe has stopped working". Here's the code:

Main.cpp:

#include <iostream>
 
//GLEW
#define GLEW_STATIC
#include<glew.h>
 
//GLFW
#include <glfw3.h>
 
#include "Shader.h"
#include "Camera.h"
#include <SOIL.h>
 
// GLM
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
 
// Properties
GLuint screenWidth = 1800, screenHeight = 1000;
 
// Function prototypes
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
void do_movement();
 
GLfloat deltaTime = 0.0f;
GLfloat lastFrame = 0.0f;
GLfloat posX = 0.0f;
GLfloat posY = 0.0f;
GLfloat zoomZ = 0.1f;
 
bool keys[1024];
// The MAIN function, from here we start our application and run our Game loop
int main()
{
// Init GLFW
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
glfwWindowHint(GLFW_SAMPLES, 4);
 
GLFWwindow* window = glfwCreateWindow(screenWidth, screenHeight, "LearnOpenGL", nullptr, nullptr); // Windowed
glfwMakeContextCurrent(window);
 
// Set the required callback functions
glfwSetKeyCallback(window, key_callback);
 
 
// Options
//glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
 
// Initialize GLEW to setup the OpenGL Function pointers
glewExperimental = GL_TRUE;
glewInit();
 
// Define the viewport dimensions
glViewport(0, 0, screenWidth, screenHeight);
 
// Setup some OpenGL options
glEnable(GL_DEPTH_TEST);
glEnable(GL_STENCIL_TEST);
// 1. Action to take when stencil test fail, 2
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
 
// Setup and compile our shaders
Shader imageShader("shader.vs", "imageShader.frag");
Shader cubeShader("shader.vs", "cubeShader.frag");
// Set up our vertex data (and buffer(s)) and attribute pointers
GLfloat vertices[] = {
//vertex position texture 
0.5f,  0.5f, 0.0f, 1.0f, 1.0f,
0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f,
-0.5f,  0.5f, 0.0f, 0.0f, 1.0f
 
};
 
GLuint indices[] = {  // Note that we start from 0!
0, 1, 3,   // First Triangle
1, 2, 3    // Second Triangle
};
 
GLuint VBO, VAO[2], EBO;
glGenVertexArrays(2, VAO); 
glGenBuffers(1, &VBO); // for vertices
glGenBuffers(1, &EBO); //for indices
 
// Bind our Vertex Array Object first, then bind and set our buffers and pointers.
glBindVertexArray(VAO[0]);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
 
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
// Position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
// TexCoord attribute
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);
glBindVertexArray(0); // Unbind VAO
 
 
glBindVertexArray(VAO[1]);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
 
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
// Position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
glBindVertexArray(0); // Unbind VAO
 // Load and create a texture 
 
GLuint texture1;
// --== TEXTURE 1 == --
glGenTextures(1, &texture1);
glBindTexture(GL_TEXTURE_2D, texture1); // All upcoming GL_TEXTURE_2D operations now have effect on our texture object
// Set our texture parameters
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// Set texture filtering
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Load, create texture and generate mipmaps
int width, height;
unsigned char* image = SOIL_load_image("space.jpg", &width, &height, 0, SOIL_LOAD_RGB);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
glGenerateMipmap(GL_TEXTURE_2D);
SOIL_free_image_data(image);
glBindTexture(GL_TEXTURE_2D, 0); // Unbind texture when done, so we won't accidentily mess up our texture.
// --== TEXTURE 2 == --
 
 
// Game loop
while (!glfwWindowShouldClose(window))
{
// Set frame time
GLfloat currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
 
// Check and call events
glfwPollEvents();
do_movement();
 
// Clear the colorbuffer
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
 
 
// Bind Textures using texture units
//glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture1);
glUniform1i(glGetUniformLocation(imageShader.Program, "ourTexture1"), 0);
 
 
 
glStencilFunc(GL_ALWAYS, 1, 0xFF); // All fragments should update the stencil buffer
glStencilMask(0xFF);
glDisable(GL_DEPTH_TEST);
 
cubeShader.Use();
glm::mat4 model;
glm::mat4 view;
glm::mat4 projection;
 
model = glm::translate(model, glm::vec3(posX, posY, -1.0f));
view = glm::lookAt(glm::vec3(0.0f, 0.0f, 3.0f), glm::vec3(0.0f, 0.0f,0.0f), glm::vec3(0.0f, 1.0f, 1.0f));
projection = glm::perspective(45.0f, (float)screenWidth / (float)screenHeight, 0.1f, 100.0f);
 
GLint modelLoc = glGetUniformLocation(cubeShader.Program, "model");
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
 
GLint viewLoc = glGetUniformLocation(cubeShader.Program, "view");
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
 
GLint projLoc = glGetUniformLocation(cubeShader.Program, "projection");
glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));
 
glBindVertexArray(VAO[1]);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
 
 
glStencilFunc(GL_EQUAL, 1, 0xFF); // All fragments should update the stencil buffer
glStencilMask(0xFF);
//glDisable(GL_DEPTH_TEST);
// Draw our first triangle
imageShader.Use();
 
model = glm::mat4();
 
model = glm::translate(model, glm::vec3(0.0f, 0.0f, zoomZ));
model = glm::scale(model, glm::vec3(4.0f, 3.0f, 0.0));
 
//GLint modelLoc = glGetUniformLocation(imageShader.Program, "model");
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));
 
 
glBindVertexArray(VAO[0]);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
 
 
 
// Swap the buffers
glfwSwapBuffers(window);
}
// Properly de-allocate all resources once they've outlived their purpose
glDeleteVertexArrays(2, VAO);
glDeleteBuffers(1, &VBO);
glfwTerminate();
return 0;
}
 
 
// Is called whenever a key is pressed/released via GLFW
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
{
if (action == GLFW_PRESS)
keys[key] = true;
else if (action == GLFW_RELEASE)
keys[key] = false;
//cout << key << endl;
if (keys[GLFW_KEY_ESCAPE])
glfwSetWindowShouldClose(window, GL_TRUE);
}
 
 
 
void do_movement()
{
GLfloat speed = 5.0f * deltaTime;
if (keys[GLFW_KEY_LEFT])
{
posX = posX - 1.0f * speed;
 
}
if (keys[GLFW_KEY_RIGHT])
{
posX = posX + 1.0f * speed;
}
if (keys[GLFW_KEY_UP])
{
posY = posY + 1.0f * speed;
}
if (keys[GLFW_KEY_DOWN])
{
posY = posY - 1.0f * speed;
}
if (keys[GLFW_KEY_W])
{
zoomZ = zoomZ + 1.0f * speed;
}
if (keys[GLFW_KEY_S])
{
zoomZ = zoomZ - 1.0f * speed;
}
}

Shader.h:
#ifndef SHADER_H
#define SHADER_H
 
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
 
#include <glew.h>
class Shader
{
public:
// The Program ID
GLuint Program;
Shader()
{
 
}
// Constructor reads and builds the shader
Shader(const GLchar* vertexPath, const GLchar* fragmentPath)
{
// 1. Retrieve the vertex/fragment source code from filePath
std::string vertexCode;
std::string fragmentCode;
std::ifstream vShaderFile;
std::ifstream fShaderFile;
vShaderFile.exceptions(std::ifstream::badbit);
vShaderFile.exceptions(std::ifstream::badbit);
try
{
vShaderFile.open(vertexPath);
fShaderFile.open(fragmentPath);
std::stringstream vShaderStream, fShaderStream;
// Read file's buffer contents into streams
vShaderStream << vShaderFile.rdbuf();
fShaderStream << fShaderFile.rdbuf();
// Close file handlers
vShaderFile.close();
fShaderFile.close();
// Convert stream into GLchar array
vertexCode = vShaderStream.str();
fragmentCode = fShaderStream.str();
}
catch (std::ifstream::failure e)
{
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ" << std::endl;
}
const GLchar* vShaderCode = vertexCode.c_str();
const GLchar* fShaderCode = fragmentCode.c_str();
 
//Compile Shaders
GLuint vertex, fragment;
GLint success;
GLchar infoLog[512];
 
//Vertex Shader
vertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex, 1, &vShaderCode, NULL);
glCompileShader(vertex);
// Print compile errors if any
glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(vertex, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
}
 
// Fragment Shader
fragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment, 1, &fShaderCode, NULL);
glCompileShader(fragment);
// Print compile errors if any
glGetShaderiv(fragment, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(fragment, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
}
 
// Shader Program
this->Program = glCreateProgram();
glAttachShader(this->Program, vertex);
glAttachShader(this->Program, fragment);
glLinkProgram(this->Program);
// Print linking errors if any
glGetProgramiv(this->Program, GL_LINK_STATUS, &success);
if (!success)
{
glGetProgramInfoLog(this->Program, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
}
glDeleteShader(vertex);
glDeleteShader(fragment);
}
// Use the program
void Use()
{
glUseProgram(this->Program);
}
};
 
#endif

I tried to debug it and it threw an exception at: glfwSetKeyCallback(window, key_callback);
If i comment it, it throws me an exception at: Shader imageShader("shader.vs", "imageShader.frag");
I don't know if it matters...the project is made in visual studio 2015 on my laptop and it works. The problem appears on another laptop with visual studio 2012.And if you're wondering, I already took the compiled libraries and include for visual studio 2012. So what could be the problem ?

Thank you!
Advertisement

"threw an exception", there are a few zillion so which one? What is the message?

It would probably not help me, as I don't use Windows, but others may get better understanding from it.

"If i comment it, it throws me an exception at ........." Well, you're dead already, postponing death by skipping code won't do much good or miraculously rise the program from the dead :p In other words, not often a useful action.

Otherwise, I'd recommend making a copy of the code, and then strip it to the bare minimum. That way you get a minimal example rather than 4 pages of code and no clue. Proper indenting helps a lot too, as now it's just random guessing which brackets match with each other.

Finally, I see very little error handling code, eg while reading shaders and all openGL code. Maybe the "vShaderFile.exceptions(std::ifstream::badbit);" takes care of of the former, although I don't see why you run that statement twice, and none for the fShaderFile.

Having proper error checking may seem useless, until you hit this kind of "it doesn't work" situation. Good luck finding out which of your calls fails now. If you check the return values, the program will die instantly, and point directly to the call with the problem. In my experience that saves you hours to days of bug-hunting, well worth the investment of adding error checking.

The debugger is your friend. Debug until the exception occurs, then inspect the variables.
Also, paste the exact exception messages. They have at least a message and a type.

My guess for the first exception, the window returned by glfwCreateWindow is NULL and therefore setting the handler fails.

My guess for the second exception, the files are not found and you get one of your own exceptions thrown.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

"Project.exe has stopped working"

Is just the stupid Windows message for his non-expert users like the secretary writing something with word at work. It says nothing we do already now and does not help at all. I think you work on Windows 10; its a shame that they screwed down all the usefull stuff from Windows 7 but raised up all the stuff we dont need or will need in next 100 years.

Run your code in debugger pressing F5 in VS is a must do for unexperienced developers and even for experienced ones to find tricky bugs and general messages. Also you should frequently qeury GetLastError function from WINAPI

Could be that glfwCreateWindow failed and returned a null pointer. Have a look at how error handling works in GLFW to find out why. Of the possible errors I suspect it be one of GLFW_INVALID_VALUE (if 4x MSAA is unavailable), GLFW_VERSION_UNAVAILABLE (if OpenGL 3.3 Core Profile is unavailable).

blah :)

Somebody told me that GPU doesn't support the libraries. That laptop with vs 2012 is a little bit old and maybe that's why is not working. :D

Somebody told me that GPU doesn't support the libraries. That laptop with vs 2012 is a little bit old and maybe that's why is not working.
Had you added checking return values of the library calls, and reporting such failures, you would have known that for sure 24 hours ago, instead of maybe guesses now.

Hi again,

So I came back with this problem. To be honestly I didn't know how to add checking return values. :P Maybe because I don't know exactly what do you mean. :P
Now I am not working on the laptop mentioned (that one that is a little bit old).I'm working with mine on an particles project. My laptop is good enough.:D So I have the same problem, when I press f11, it gives me the error at a line that is: vertex = glCreateShader(GL_VERTEX_SHADER);

The error:
Exception thrown at 0x00000000 in ParticlesGenerator.exe: 0xC0000005: Access violation executing location 0x00000000.
If there is a handler for this exception, the program may be safely continued.

The Shader class is the same as the one mentioned above. If you need the code for the particles project, just let me know. :P
Thank you!

https://stackoverflow.com/questions/12329082/glcreateshader-is-crashing

According to this SO article, glew and glfw interfere with each other.

That was the second hit from just googling "glCreateShader Access Violation", googling your error messages does wonders.

I did it. I moved the shader creation in main function. :D
Thank you all for your support. :P

This topic is closed to new replies.

Advertisement