Posted: Sun May 29, 2016 1:17 pm Post subject:
C++ weird exception.
Subject description: SDL & OpenGl 2d game engine
Hi all, before i begin i MUST tell you that i already asked this question at Stack-overflow and Game.devs , but the guys there didn't give me answer, they just fixed my grammar.
Exception thrown at 0x00000000 in Black Engine.exe: 0xC0000005: Access violation executing location 0x00000000.
Unhandled exception at 0x00000000 in Black Engine.exe: 0xC0000005: Access violation executing location 0x00000000.
I even Copy/pasted Sprite.cpp and Sprite.h from his source code but the problem persisted.
sprite.cpp
Code:
//Initializes the sprite VBO. x, y, width, and height are
//in the normalized device coordinate space. so, [-1, 1]
void Sprite::init(float x, float y, float width, float height) {
//Set up our private vars
_x = x;
_y = y;
_width = width;
_height = height;
//Generate the buffer if it hasn't already been generated
if (_vboID == 0) { //<--------Problem starts here
glGenBuffers(1, &_vboID);
}
//This array will hold our vertex data.
//We need 6 vertices, and each vertex has 2
//floats for X and Y
float vertexData[12];
//First Triangle
vertexData[0] = x + width;
vertexData[1] = y + height;
vertexData[10] = x + width;
vertexData[11] = y + height;
//Tell opengl to bind our vertex buffer object
glBindBuffer(GL_ARRAY_BUFFER, _vboID);
//Upload the data to the GPU
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW);
//Unbind the buffer (optional)
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
//Draws the sprite to the screen
void Sprite::draw() {
//bind the buffer object
glBindBuffer(GL_ARRAY_BUFFER, _vboID);
//Tell opengl that we want to use the first
//attribute array. We only need one array right
//now since we are only using position.
glEnableVertexAttribArray(0);
//Point opengl to the data in our VBO. We will learn
//more on this later
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
//Draw the 6 vertices to the screen
glDrawArrays(GL_TRIANGLES, 0, 6);
//Disable the vertex attrib array. This is not optional.
glDisableVertexAttribArray(0);
//Unbind the VBO
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
this what happen in order:
1- There is no error in Error list (Intellisense)
2- debugging started
3- Compiled successfully
4- launching program
5- Console logs successfully
6- SDL & OpenGL "glew" has been initialized
7- Window created successfully
8- KABOOOM exception arises.
i even tried to make an handler for this exception, but somehow it don't work.
what i did so far:
-i noticed the the problem is in any function that uses _VBOID which is GLuint type
-i allocated _VBOID in the heap
-Copy/pasted the source code from the tutorial, but nothing works.
i've tried everything in Sprite.cpp and nothing works, it always gives me exception. and if i commented out this part, the exception will arises in another part.
VBOID is a pointer to a GLuint, not a GLuint itself, you code for checking it should be:
if (*_vboID == 0) {
glGenBuffers(1, _vboID);
}
However I don't get why you are allocating this on the heap, just make vboID a stack allocated member and you can forget about the memory handling and use the syntax you use in your example. QUICK_EDIT
VBOID is a pointer to a GLuint, not a GLuint itself, you code for checking it should be:
if (*_vboID == 0) {
glGenBuffers(1, _vboID);
}
However I don't get why you are allocating this on the heap, just make vboID a stack allocated member and you can forget about the memory handling and use the syntax you use in your example.
i did what you told me to do, and the problem wasn't solved as exception arose again in
if it matters, i fixed it by doing hell of research and google.
Problem fix:
1- SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); must be before creating the window, but in his tutorial he added it after after creation of window.
I wonder, how did he succeeded in compiling and launching the program, even if he has errors in his code. _________________
You don't even need that call to SetAttribute to set GL double buffering, according to the SDL2 wiki, it has that default to 1 anyhow. What you should do is check the attribute value with GetAttribute first and only then set it if it isn't what you expected.
It also still doesn't fix the fact that you weren't passing pointers to stuff around correctly either. QUICK_EDIT
You don't even need that call to SetAttribute to set GL double buffering, according to the SDL2 wiki, it has that default to 1 anyhow. What you should do is check the attribute value with GetAttribute first and only then set it if it isn't what you expected.
It also still doesn't fix the fact that you weren't passing pointers to stuff around correctly either.
well, In the beginning _VBOID was just a GLuint type, when i faced this exception i tried everything to make the Engine work. i even tried to make _VBOID pointer to GLuint type, ("I never understood some advantages of using pointer").
When the problem was fixed, i returned _VBOID to GLuint type.
I still have a problem with compiling the fragment shader, but i am trying to fix it. _________________
Sounds to me like you need a better grasp of the basics, I would work through at least some of http://c.learncodethehardway.org/ first or something to get a base understanding of C and particularly pointers and what to use heap and stack allocation for.
In general, don't mess around with heap memory allocation unless you really know you need to, you shouldn't do it as part of trying to troubleshoot code that should work without it. QUICK_EDIT
Sounds to me like you need a better grasp of the basics, I would work through at least some of http://c.learncodethehardway.org/ first or something to get a base understanding of C and particularly pointers and what to use heap and stack allocation for.
In general, don't mess around with heap memory allocation unless you really know you need to, you shouldn't do it as part of trying to troubleshoot code that should work without it.
I know C++ , but not very pro at it.
Isn't using new has an advantage over malloc that it can call constructor and delete call its destructor?
i read about C++ dynamic memory allocation, and maybe one day i will fully understand it.
If you don't known when, where or why to use it, don't use it. A lot of C++ guys will tell you not to use it at all and rely on the data structures provided in the standard libraries such as vectors to allocate and manage chunks of memory as they provide additional guarantees for exception safety and such. QUICK_EDIT
You can post new topics in this forum You can reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum