Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

878009 Posts in 32898 Topics- by 24324 Members - Latest Member: Fi_designer

May 21, 2013, 01:52:44 AM
TIGSource ForumsDeveloperTechnical (Moderators: Glaiel-Gamer, ThemsAllTook)access violation std::vector.push_back()
Pages: [1]
Print
Author Topic: access violation std::vector.push_back()  (Read 524 times)
milo_dercius
Level 0
**



View Profile Email
« on: August 31, 2012, 12:32:19 PM »

I have a function
Code:
void GenerateBlockBuffer(Vertex* vertices, int* indices, int vertexCount, int indexCount)
{
VertexBuffer vb;
vb.vertexCount = vertexCount;

IndexBuffer ib;
ib.indexCount = indexCount;

glGenBuffers(1, (GLuint*)&vb.id);

glBindBuffer(GL_ARRAY_BUFFER, vb.id);
glBufferData(GL_ARRAY_BUFFER, vb.vertexCount * 7 * sizeof(GLfloat), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

glGenBuffers(1, (GLuint*)&ib.id);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ib.id);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, ib.indexCount * sizeof(GLint), indices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

blockVertexBuffers.push_back(vb);
blockIndexBuffers.push_back(ib);
}

and it crashes on
Code:
blockIndexBuffers.push_back(ib);

with: 0xC0000005: Access violation reading location 0x00000019, when ib.id = 164.
The VertexBuffer and IndexBuffer structs each have an int that stores the buffer id and an int that stores how many vertices or indices are in the buffer. I do not understand what causes it to crash.

EDIT: It compiles and runs fine in release but not debug.
« Last Edit: August 31, 2012, 02:42:54 PM by milo_dercius » Logged
Liosan
Level 2
**


View Profile
« Reply #1 on: August 31, 2012, 02:09:07 PM »

Is this==null or something?

Liosan
Logged

milo_dercius
Level 0
**



View Profile Email
« Reply #2 on: August 31, 2012, 02:10:52 PM »

It is global.
Logged
ASnogarD
Level 1
*



View Profile
« Reply #3 on: August 31, 2012, 02:50:29 PM »

I hate to even suggest this as I have no idea of opengl code at all, but one thing I would try or look at is to use new to allocate memory for the indexbuffer and vertexbuffer...

as in something like :

vertexbuffer ib = new vertexbuffer;

just dont forget to delete before you clear the element on the vector.

... either that or your vertexbuffer is accessing none initialised and garbage sections of memory, perhaps a data member not being initialised ?

I hate trying to 'help' programmers who are on subjects ahead of me, but hate it if that was the problem and I just didnt type out a few lines to help... so ignore me if I am talking rubbish  Tongue
Logged

Somethings are painfully obvious, others must be made obvious... painfully.
milo_dercius
Level 0
**



View Profile Email
« Reply #4 on: August 31, 2012, 03:06:56 PM »

I avoided/solved the problem by not having a std::vector of VertexBuffers and IndexBuffers but instead storing them in the chunk class. My game is like minecraft and has a world made of cubes ( soon to be hexahedrons ) stored in chunks. It's more organized to keep them like that anyway. I guess the problem was somehow caused by having my vectors in global scope.
Logged
eigenbom
Level 10
*****



View Profile WWW
« Reply #5 on: August 31, 2012, 03:07:35 PM »

Some tips:
- You can use OpenGL's error checking to help with debugging
- Can you inspect the block* data structures at the time of the crash in a debugger, that might help
Good luck!
Logged

eigenbom
Level 10
*****



View Profile WWW
« Reply #6 on: August 31, 2012, 03:10:46 PM »

I guess the problem was somehow caused by having my vectors in global scope.

hmm .. that's weird and shouldn't have been the problem. you may still be having memory corruption, just not a memory access violation..
Logged

motorherp
Level 2
**



View Profile WWW Email
« Reply #7 on: August 31, 2012, 03:15:13 PM »

I guess the problem was somehow caused by having my vectors in global scope.

Do you have one global object calling methods of or accessing another global object in its contructor?  This could be a problem caused by the fact that you cant predict the order of global initialisation.
Logged

Average Software
Level 10
*****

Fleeing all W'rkncacnter


View Profile WWW Email
« Reply #8 on: August 31, 2012, 07:38:12 PM »

I guess the problem was somehow caused by having my vectors in global scope.

Do you have one global object calling methods of or accessing another global object in its contructor?  This could be a problem caused by the fact that you cant predict the order of global initialisation.

You certainly can predict it, there's just a good chance you'll be wrong.
Logged

Franchise - The restaurant wars begin!

What would John Carmack do?
motorherp
Level 2
**



View Profile WWW Email
« Reply #9 on: September 01, 2012, 03:11:05 AM »

Hehe true  Big Laff
Logged

Klaim
Level 10
*****



View Profile WWW
« Reply #10 on: September 01, 2012, 05:10:12 AM »

In my experience, there is two ways this can happen:

 1. your IndexBuffer does more than you say. Show the class definition to us otherwise we can't help on this.
 2. you have a buffer overflow somewhere.

The 2 point makes possible for some part of the code to do something wrong in your vector data.


First show use IndexBuffer interface (and the rest if you can).


Remember that C++ code is highly contextual. It is better to try to reproduce a problem in a simple C++ project in this kind of case.

Also, this cannot be caused by OpenGL directly as your error is in manipulating IndexBuffer and std::vector.
Logged

http://www.klaimsden.net | Game : NetRush | Digital Story-Telling Technologies : Art Of Sequence
Endurion
Level 1
*



View Profile WWW Email
« Reply #11 on: September 02, 2012, 05:57:30 AM »

When storing objects in vectors, every push_back creates a copy.

As Klaim said, show the code of those buffer classes. For example, do you do anything weird in the destructor?
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic