Project Perfect Mod Forums
:: Home :: Get Hosted :: PPM FAQ :: Forum FAQ :: Privacy Policy :: Search :: Memberlist :: Usergroups :: Register :: Profile :: Log in to check your private messages :: Log in ::


The time now is Thu Mar 28, 2024 7:16 pm
All times are UTC + 0
C++ weird exception.
Moderators: Global Moderators
Post new topic   Reply to topic Page 1 of 1 [13 Posts] Mark the topic unread ::  View previous topic :: View next topic
Author Message
temp
Rocket Cyborg


Joined: 17 Oct 2015

PostPosted: Sun May 29, 2016 1:17 pm    Post subject:  C++ weird exception.
Subject description: SDL & OpenGl 2d game engine
Reply with quote  Mark this post and the followings unread

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.

So, i am creating a 2D game engine using this https://www.youtube.com/watch?v=LLUtSE2osfI&list=PLSPw4ASQYyymu3PfG9gxywSPghnSMiOAW&index=10
i am in episode 8 about Shaders, but the problem Began in episode 6 in Sprite.cpp i always get an Exception
Code:
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[2] = x;
    vertexData[3] = y + height;

    vertexData[4] = x;
    vertexData[5] = y;

    //Second Triangle
    vertexData[6] = x;
    vertexData[7] = y;

    vertexData[8] = x + width;
    vertexData[9] = y;

    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);
}

_________________
Lin Kuei Ominae wrote:
@Pussypus Talking to a wall is more fertile. Face palm!  Wall bash!

Back to top
View user's profile Send private message
CCHyper
Defense Minister


Joined: 07 Apr 2005

PostPosted: Sun May 29, 2016 1:53 pm    Post subject: Reply with quote  Mark this post and the followings unread

Have you tried debugging your application?

Back to top
View user's profile Send private message
temp
Rocket Cyborg


Joined: 17 Oct 2015

PostPosted: Sun May 29, 2016 2:27 pm    Post subject: Reply with quote  Mark this post and the followings unread

CCHyper wrote:
Have you tried debugging your application?


yes, i did.

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. #Mad  #Mad  #Mad  #Mad  Wall bash!  Wall bash!  Wall bash!  Wall bash!  Ranting!  Ranting!  Ranting!  Ranting!

source: https://github.com/Z-organization/Black-Engine
if it matters,i have evga Geforce 210 "1 gb" with support of Opengl 3.3

_________________
Lin Kuei Ominae wrote:
@Pussypus Talking to a wall is more fertile. Face palm!  Wall bash!

Back to top
View user's profile Send private message
Blade
Cyborg Commando


Joined: 23 Dec 2003

PostPosted: Sun May 29, 2016 10:56 pm    Post subject: Reply with quote  Mark this post and the followings unread

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.

Back to top
View user's profile Send private message
temp
Rocket Cyborg


Joined: 17 Oct 2015

PostPosted: Sun May 29, 2016 11:16 pm    Post subject: Reply with quote  Mark this post and the followings unread

Blade wrote:
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
Code:
glBindBuffer(GL_ARRAY_BUFFER, *_VBOID);

_________________
Lin Kuei Ominae wrote:
@Pussypus Talking to a wall is more fertile. Face palm!  Wall bash!

Back to top
View user's profile Send private message
temp
Rocket Cyborg


Joined: 17 Oct 2015

PostPosted: Mon May 30, 2016 2:07 am    Post subject: Reply with quote  Mark this post and the followings unread

At last, it worked!!!!!

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.

_________________
Lin Kuei Ominae wrote:
@Pussypus Talking to a wall is more fertile. Face palm!  Wall bash!

Back to top
View user's profile Send private message
Blade
Cyborg Commando


Joined: 23 Dec 2003

PostPosted: Mon May 30, 2016 10:59 am    Post subject: Reply with quote  Mark this post and the followings unread

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.

Back to top
View user's profile Send private message
temp
Rocket Cyborg


Joined: 17 Oct 2015

PostPosted: Mon May 30, 2016 12:47 pm    Post subject: Reply with quote  Mark this post and the followings unread

Blade wrote:
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.

_________________
Lin Kuei Ominae wrote:
@Pussypus Talking to a wall is more fertile. Face palm!  Wall bash!

Back to top
View user's profile Send private message
Blade
Cyborg Commando


Joined: 23 Dec 2003

PostPosted: Mon May 30, 2016 1:36 pm    Post subject: Reply with quote  Mark this post and the followings unread

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.

Back to top
View user's profile Send private message
temp
Rocket Cyborg


Joined: 17 Oct 2015

PostPosted: Mon May 30, 2016 3:10 pm    Post subject: Reply with quote  Mark this post and the followings unread

Blade wrote:
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.

Thanks, Blade.

_________________
Lin Kuei Ominae wrote:
@Pussypus Talking to a wall is more fertile. Face palm!  Wall bash!

Back to top
View user's profile Send private message
Banshee
Supreme Banshee


Also Known As: banshee_revora (Steam)
Joined: 15 Aug 2002
Location: Brazil

PostPosted: Tue May 31, 2016 6:06 am    Post subject: Reply with quote  Mark this post and the followings unread

Blade has mentioned that you need to learn how memory allocation and pointers works.

Back to top
View user's profile Send private message Visit poster's website Skype Account
temp
Rocket Cyborg


Joined: 17 Oct 2015

PostPosted: Tue May 31, 2016 11:39 am    Post subject: Reply with quote  Mark this post and the followings unread

Banshee wrote:
Blade has mentioned that you need to learn how memory allocation and pointers works.


i know how they work, but i don't know where (or when) to use them.

_________________
Lin Kuei Ominae wrote:
@Pussypus Talking to a wall is more fertile. Face palm!  Wall bash!

Back to top
View user's profile Send private message
Blade
Cyborg Commando


Joined: 23 Dec 2003

PostPosted: Tue May 31, 2016 4:14 pm    Post subject: Reply with quote  Mark this post and the followings unread

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.

Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [13 Posts] Mark the topic unread ::  View previous topic :: View next topic
 
Share on TwitterShare on FacebookShare on Google+Share on DiggShare on RedditShare on PInterestShare on Del.icio.usShare on Stumble Upon
Quick Reply
Username:


If you are visually impaired or cannot otherwise answer the challenges below please contact the Administrator for help.


Write only two of the following words separated by a sharp: Brotherhood, unity, peace! 

 
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


Powered by phpBB © phpBB Group

[ Time: 0.1633s ][ Queries: 11 (0.0078s) ][ Debug on ]