-1

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()

genpfault
  • 51,148
  • 11
  • 85
  • 139
Nooble
  • 562
  • 7
  • 17
  • I suspect you did debug it, keeping watch at variable and pointer values, didn't you? – πάντα ῥεῖ Sep 21 '14 at 04:09
  • what do you mean by "if I x out the console" – bjskishore123 Sep 21 '14 at 04:09
  • Err, instead of letting the program flow normally by entering through my artificial pause at the end (getchar()) I press the X on the window. You know, minimize, maximixe, X. – Nooble Sep 21 '14 at 04:11
  • 1
    @Nooble FYI, this is undefined behavior if the vector is empty: `&Vertices.front()` – PaulMcKenzie Sep 21 '14 at 04:12
  • @PaulMcKenzie yes, but in this case, it is not empty. It is loaded with 9 floats. – Nooble Sep 21 '14 at 04:13
  • 2
    @Nooble - `I tried to narrow it down to what I think is causing the violation at 0xFEEEFEEE` Did you run this program under the debugger, and if it crashed, look at the call stack to see what functions were called that led to the error? Also, please look at this link to the Microsoft magic number errors: http://stackoverflow.com/questions/127386/in-visual-studio-c-what-are-the-memory-allocation-representations The error more than likely you are accessing memory that was already marked as "freed" by the MS runtime. – PaulMcKenzie Sep 21 '14 at 04:20
  • IIRC clicking the close button on the console window generates a signal which is caught by the C runtime, whose handler for it performs some cleanup tasks before terminating the program. Those cleanup can cause a race with the rest of your program - one possible manifestation is the access violation. – T.C. Sep 21 '14 at 04:25

1 Answers1

1

One note about your program -- you're recreating all your vertex arrays and programs and such and recompiling the shaders every time you call Draw. If you're doing that in your main loop, that means your redoing it on every frame, which will be extremely slow. The whole point of GL objects is that you set them up once when the program starts and then reuse them each time you draw. So your Draw function should be:

void Mesh::Draw() {
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBindVertexArray(VAO);
    glUseProgram(shader_programme);
    glDrawArrays(GL_TRIANGLES, 0, 3);
}

All the rest of the setup stuff should be in the constructor.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226