opengl - C++: vector size is wrong and higher than the number of elements -


in attempt load .obj-files load vertex data std::vectors send them gpu later. fill 3 vectors, normals, vertices , texture coordinates. size of normal vector far higher size of other 2 vectors, if filled same number of elements.

code:

scenetree* generatescenetree(objscene* scene){     postprocessing::triangulatefaces(scene);     scenenode* node = new scenenode;      vector<vec<3>>& scenenormals = scene->attributedata[attribute::normal];     vector<vec<3>>& scenevertices = scene->attributedata[attribute::position];     vector<vec<3>>& scenetexcoords = scene->attributedata[attribute::texcoord];      map<string,materialinfo*> mtls;     for(string s : scene->mtllibs){         auto temp = loadmtl(s);         mtls.insert(temp.begin(),temp.end());     }      vector<vec<3>> meshnormals;      <-- creating vectors here.     vector<vec<3>> meshvertices;     vector<vec<2>> meshtexcoords;     for(auto g : scene->groups){         meshnormals.clear();             meshnormals.reserve(g.faces.size()*3);         meshvertices.clear();             meshvertices.reserve(g.faces.size()*3);         meshtexcoords.clear();             meshtexcoords.reserve(g.faces.size()*3);         aabb bbox;         cout << "num of faces: " << g.faces.size() << endl;         for(auto f : g.faces){             for(auto p : f.points){                 uint vindex = p.indices[attribute::position];                 uint nindex = p.indices[attribute::normal];                 uint tindex = p.indices[attribute::texcoord];                  vec<3> n = scenenormals.at(nindex);                 vec<3> v = scenevertices.at(vindex);                 vec<3> t = scenetexcoords.at(tindex);                  meshnormals.push_back(n);                 meshvertices.push_back(v);                 meshtexcoords.push_back(t.tovec<2>());                  bbox += meshvertices.back();             }         }         cout << "meshnormals size: " << meshnormals.size() << endl;         cout << "meshvertices size: " << meshvertices.size() << endl;         cout << "meshtexcoords size: " << meshtexcoords.size() << endl;         mesh* m = new mesh({             {meshvertices,attribute::position},             {meshnormals,attribute::normal},             {meshtexcoords,attribute::texcoord}         },gl_triangles);         sceneleaf* leaf = new sceneleaf;         leaf->nodedata = {matrix4(),bbox};         leaf->leafdata = {m, mtls[g.mtlname]};         node->addchild(leaf);     }     return node; } 

output:

num of faces: 1087474 meshnormals size: 2958875950 meshvertices size: 3262422 meshtexcoords size: 3262422 

this seems highly illogical. program crashs afterwards std::bad_array_new_length exception because mesh class cant create array of size 2958875950 send gpu.

update:

if swap declarations of meshvertices , meshnormals, meshvertices has wrong size. first created vector affected.

if use std::list instead of std::vector, works.

if comment out ....reserve(g.faces.size()*3); lines, std::bad_alloc thrown.

my guess have memory corruption bug "somewhere" overwriting meshnormals variable on stack. fact swapping meshnormals , meshvertices declarations leads meshvertices becoming bad matches theory.

to narrow in on problem can few things:

  1. comment out lines in inner for(auto p : f.points) loop , see if error still occurs.
  2. assuming doesn't, start uncommenting lines one-by-one until error shows again.
  3. try making minimal, stand-alone test code example duplicates problem (it immensely if did before posting question).

Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -