Note: I have already fixed this issue by using a vector of vectors instead of an array of vectors, but I'm still curious about why the program crashed by SegFault.
In my program I have to store the meshes of multiple object types (2, in this case) as floats in vectors in order to render them using OpenGL. I had decided that reading them using ifstream and storing them in an array of std::vectors would do just fine. Here's how the loading sequence looked like:
int ObjAmount=2;
char* loadloc[ObjAmount];
//loadloc stores the paths of the files that store the meshes
loadloc[0]="IntegralHead.raw":
loadloc[1]="Pearl.raw";
vector <float> model[ObjAmount];
float modelvar;
for(int i=0; i<ObjAmount; i++)
{
ifstream modelread (loadloc[i]);
while(modelread>>modelvar)
model[i].push_back(modelvar);
}
And here's how passing to the buffers looks like:
unsigned int VBO2[ObjAmount], VAO2;
glGenVertexArrays(1, &VAO2);
glBindVertexArray(VAO2);
for(int i=0; i<ObjAmount; i++)
{
glGenBuffers(1, &VBO2[i]);
glBindBuffer(GL_ARRAY_BUFFER, VBO2[i];
glBufferData(GL_ARRAY_BUFFER, model[i].size()*sizeof(float), &model[i][0], GL_STATIC_DRAW);
}
The program worked just fine when I didn't turn on optimizations, but it crashed by segmentation fault in a function from atioglxx.dll
when turning on the -O1
, -O2
, -O3
or -Os
, when reaching the glBufferData
call.
Any idea why the crash happened?