Access Violation GLEW/GLFW3

Hello,

I have the issue when following this tutorial

However when I do this:


unsigned int vbo = 0;
glGenBuffers (1, &vbo);

I get this:


Unhandled exception at 0x777ABDA1: 0xC0000005: Access violation executing location 0x00000000.

And I have initiated glfw and glew…

Any Suggestions?

Many Thanks

DoctorZeus

Most likely glGenBuffers is not a valid function pointer, so the call fails. Possibly glew is not correctly initialized/encounters an error while initializing (do you set glewExperimental=TRUE before glewInit?) or your context creation does not work correctly.
You’ll have to post your code where you create the OpenGL context and initialize glew to say anything more specific.

[QUOTE=carsten neumann;1253550]Most likely glGenBuffers is not a valid function pointer, so the call fails. Possibly glew is not correctly initialized/encounters an error while initializing (do you set glewExperimental=TRUE before glewInit?) or your context creation does not work correctly.
You’ll have to post your code where you create the OpenGL context and initialize glew to say anything more specific.[/QUOTE]

Thanks for the reply,

Yes I have set glewExperimental to true before it is init…

Here is my code to init and create a window:


bool CreateGraphicsWindow(unsigned int WindowSizeX, unsigned int WindowSizeY, char* WindowName = (char*)"MAIN WINDOW", bool fullScreen = false)
    {
	glewExperimental=true;
        if (!glewInit() || !glfwInit())
        {
            TerminateNReturn((char*)"CANNOT INITALIZE GRAPHICS!");
            return false;
        }

        if (fullScreen)
            GraphicsWindow = glfwCreateWindow(WindowSizeX, WindowSizeY, WindowName, glfwGetPrimaryMonitor(), NULL);
        else
            GraphicsWindow = glfwCreateWindow(WindowSizeX, WindowSizeY, WindowName, NULL, NULL);
        if (!GraphicsWindow)
        {
            TerminateNReturn((char*)"CANNOT CREATE WINDOW");
            return false;
        }
		
        glfwMakeContextCurrent(GraphicsWindow);

        return true;
    }

Many Thanks

DoctorZeus

You have to initialize GLFW first. You need an active OpenGL context before you can load OpenGL functions or make any OpenGL calls.

This means you need to initialize GLFW, set up your window properties, and then create a window. All before touching GLEW or anything in OpenGL.

[QUOTE=Alfonse Reinheart;1253560]You have to initialize GLFW first. You need an active OpenGL context before you can load OpenGL functions or make any OpenGL calls.

This means you need to initialize GLFW, set up your window properties, and then create a window. All before touching GLEW or anything in OpenGL.[/QUOTE]

Correct me if I am wrong (Noob alert with opengl here) but isnt glfw inialized with glfwinit() and the window created with glfwCreateGraphicsWindow?

I can render basic shapes by specifying the coords using glBegin and glEnd and these render fine when entered in manually (I can render basic cubes and etc, etc…

Thanks

DoctorZeus

Correct me if I am wrong (Noob alert with opengl here) but isnt glfw inialized with glfwinit() and the window created with glfwCreateGraphicsWindow?

Yes. And your call to “glewInit” comes before all of that. You have to initialize GLEW after creating the window, not before.

I can render basic shapes by specifying the coords using glBegin and glEnd and these render fine when entered in manually (I can render basic cubes and etc, etc…

That’s because GLEW’s macros pipe the GL 1.1 stuff through non-GLEW functions. Any functions from later GL versions will fail because GLEW has not been properly initialized yet.

[QUOTE=Alfonse Reinheart;1253563]Yes. And your call to “glewInit” comes before all of that. You have to initialize GLEW after creating the window, not before.

That’s because GLEW’s macros pipe the GL 1.1 stuff through non-GLEW functions. Any functions from later GL versions will fail because GLEW has not been properly initialized yet.[/QUOTE]

Ok that worked thanks! :slight_smile:

However I cannot see anything being rendered so will have a look into this!

Many Thanks! :slight_smile:

DoctorZeus

Again many thanks to everyone for their help…

One more question, can someone please explain to me why this doesn’t work (causes another access violation at a memory address, alsopTR->GetVertexes() returns a std::vector<float>)?

    void AddObjectToRender(GraphicsStruc_Model *pTR)
    {
		GLuint vertexBuffer;
		if (pTR->ISMODELLOADED())
		{
			glEnableClientState(GL_VERTEX_ARRAY);

			glVertexPointer(3, GL_FLOAT, 0, &pTR->GetVertexes());
			glDrawArrays(GL_TRIANGLES, 0, pTR->GetVertexes().size());

			glDisableClientState(GL_VERTEX_ARRAY);
		}
    }

I’m pretty sure it has something to do with vertex buffers and allocating memory but am totally lost in this regard… :frowning:

Could someone please be kind enough and explain it to me?

Kind Regards

DoctorZeus

Nothing to do with OpenGL directly. You are passing the address of a std::vector<float> to glVertexPointer, which is the address of the vector not the data it stores (which is what you want to pass to OpenGL) - you’d want &(pTR->GetVertexes().front()) for example. Additionally, your function returns the std::vector<float> by value (i.e. it makes a copy), so you are taking the address of a temporary that is deleted immediately after the glVertexPointer call - it’s also inefficient because you keep copying the potentially large vertex data. Either assign the return value from GetVertexes() to a local variable (which still makes the copy, but keeps it alive until the end of scope):


std::vector<float> v = pTR->GetVertexes();
glVertexPointer(3, GL_FLOAT, 0, &v.front());

Or change your function to return a const reference (but make sure you understand what that does!):


struct GraphicsStruc_Model
{
    const std::vector<float>& GetVertexes(void);
}

If any of this seems confusing, I’d recommend reading up on C++ object live time and memory management, you are likely to keep running into similar problems otherwise.

Thanks for the reply! :slight_smile:

Luckily it all seems to make sense :slight_smile: , I take it we obviously don’t want it to create a new vector with a different memory address (as we want it to point to the start of the vector) so it is declared as constant. Also we don’t want duplicated data in the memory.! :slight_smile:

Couldn’t you also do this:

bool AddObjectToRender(GraphicsStruc_Model *pTR)
    {
		GLuint numVertexes, numNormals, numTexturePoints;

		if (pTR->ISMODELLOADED())
		{
			const float verteciesFront = pTR->GetVertexesFront();
			numVertexes = pTR->GetVertexSize();


			glEnableClientState(GL_VERTEX_ARRAY);

			glVertexPointer(3, GL_FLOAT, 0, &verteciesFront);
			glDrawArrays(GL_TRIANGLES, 0, numVertexes);

			glDisableClientState(GL_VERTEX_ARRAY);

			return true;
		}

		return false;
    }

class GraphicsHandle
{
        std::vector<float> vertexPoints, normalsPoints, texturePoints;
        const float GetVertexesFront()
	{
		return vertexPoints.front();
	}
};

Unfortunately I am still having this issue, however it does work with some models! (Usually very small ones)…

Of course I may be completely wrong! :stuck_out_tongue:

Kind Regards

Doctorzeus

The above returns a copy of the first element in the vector. You want to return (and store) either a reference or pointer to the first element in the vector. The fact that this isn’t immediately obvious to you indicates that you’re still at a very early stage in learning C++. You need to learn C++ first before you start trying to learn how to use OpenGL (or anything else) in a C++ program.

Hmm, this is moving off-topic and into how pointers, memory, and object life time in C++ work and I really think you’d make your life easier if you’d review those issues outside of your concrete problem. Graphics programming is to a good extent about managing the memory for your assets.

The front() function for std::vector returns (a reference to) the first element in the vector which you then assign to the local variable verteciesFront. You then pass the address of that local variable to glVertexPointer, which is very different from passing the address of the first element in the vector.

Well keeping in mind at that stage I had been up for the last 48 hours…

[QUOTE=carsten neumann;1253665]Hmm, this is moving off-topic and into how pointers, memory, and object life time in C++ work and I really think you’d make your life easier if you’d review those issues outside of your concrete problem. Graphics programming is to a good extent about managing the memory for your assets.

The front() function for std::vector returns (a reference to) the first element in the vector which you then assign to the local variable verteciesFront. You then pass the address of that local variable to glVertexPointer, which is very different from passing the address of the first element in the vector.[/QUOTE]

I have been reviewing it since you suggested it, it has been a while since I have had to do memory management properly…

I changed it to this:

float *GetVertexVectorPoint()
	{
		return &vertexPoints[0];
	}

Many Thanks! :slight_smile:

DoctorZeus

Hi Alfonse Reinheart…could you help me please! I’ve been stuck on this for hours…I’m having the same access violation when calling glGenbuffers. I made sure I followed these steps exactly (initializing glfw, setting window properties, then creating the window before glew calls) and what I’ve done is in line with everything else I’ve found online. I’m rather new to openGL so I’m really lost as to what could be going wrong.

This is how I’ve written it:
glfwInit();
glewExperimental = GL_TRUE;
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

GLFWwindow* window = glfwCreateWindow(1200, 800, "Game", nullptr, nullptr); // Windowed

glewInit();


typedef void(*GENBUFFERS) (GLsizei, GLuint*);

// Load address of function and assign it to a function pointer
GENBUFFERS glGenBuffers = (GENBUFFERS)wglGetProcAddress("glGenBuffers");

// Call function as normal
GLuint vertexBuffer;
glGenBuffers(1, &vertexBuffer);

printf("%u

", vertexBuffer);

Any help would be greatly appreciated!