View Full Version : c++ vectors - annoying bug

02-23-2005, 05:11 PM
I'm at a loss with this... can anyone see any errors or stupid mistakes?
It compiles, but I get an illegal operation (causes an invalid page fault in MSVCRT.DLL) when I run the exe, in the eighth iteration of the loop, between "pushed back" and "fread..."
I would say the fread is reading to somewhere it shouldn't but have no idea why.
"cmesh" is a class, "cmesh->v" is a e_Vertex vector, "cmesh->v.object" is an e_Vector3f struct, new_vertex is a e_Vertex

struct e_Vector3f{
float x,y,z;

struct e_Vertex{
e_Vector3f object;
e_Vector3f world;
e_Vector2f mapcoord;

for(i=0;i<l_qty;i++){//read vertices
dumpstr("pushed back...");

Previously I used malloc with it working fine, but wanted to use vectors as they should be simpler. :(
(I haven't used [code] tags as they might not cope with the [i] in my code :()
any ideas would be great! thanks :-)

02-23-2005, 05:32 PM
Okay..something weird: It works if I read into the new_vertex THEN push it back, but not as in the above code, where I push back a new_vertex then read into it...
I guess I don't really need help now, but if anyone can see a simple mistake I would be interested... :confused:

EDIT: Is there some problem with reading into a vector? :confused:


Now THIS code doesn't work. "v" is a vector....

02-23-2005, 10:02 PM
Reading into a a vector is fine, although the normal way to do is is to read your data into your struct then push_back the struct. Either way it should't be a problem, but it's usually easier to read the code when you do it the 'normal' way.

One thing I do see though is that unless you made a typo you aren't allocating enough space for the data.. You said "new_vertex is a e_Vector3f" but shouldn't new_vertex be a "e_Vertex" instead? You need to vector.push_back(e_Vertex) not vector.pust_back(e_Vector3f).

02-23-2005, 11:26 PM
Sorry, that was a typo new_vertex is an e_Vertex :rolleyes: thanks *edits post*
I really don't understand this...in my second post U and V are both definitely floats, and from the previous code working, I know cmesh->v is valid...and it crashes before any iterations of the loop. on the first fread..

02-24-2005, 12:16 AM
I think you'll have to post more of your code to figure out the problem, because writing to a vector isn't a problem. How is cmesh allocated? What is the file format you're reading from? What mode was the file opened in? etc...

Also, if you haven't done so yet, you should build a debug version and run it in the debugger :)

02-25-2005, 07:27 PM
cmesh is a pointer to another MESH which is a vector (it IS okay to have a vector of a class, which in turn contains vectors..and so on?)
File format is .3ds, opened in "rb" mode.
a weird thing is that after the "for" in the first code, cmesh->v.size() becomes a huge number (2105656) when it should be (in my test) 286.
But even though, my code should get up to 286 "for" iterations before crashing, but doesn't crashes on the first fread...

As for debugging, i'm new to this...as far as I can see the problem occurs in this part of the stl_vector.h (at the last line dev-cpp throws an "access violation -segmentation fault" message)...

// element access
* @brief Subscript access to the data contained in the %vector.
* @param n The index of the element for which data should be accessed.
* @return Read/write reference to data.
* This operator allows for easy, array-style, data access.
* Note that data access with this operator is unchecked and
* out_of_range lookups are not defined. (For checked lookups
* see at().)
operator[](size_type __n) { return *(begin() + __n); }

02-25-2005, 11:18 PM
Well it's obvious that the vector is invalid.. probably the cmesh pointer that contains it that's bad. In your debugger you can step back through the stack and see what values the local/global variables have, and you could also place a watch on the cmesh pointer to follow it througout creation.

Make sure cmesh actually is pointing somewhere valid.
MESH *cmesh = NULL;
cmesh = new MESH; or cmesh = &existing_mesh;
if( cmesh != NULL) //were good

Other than that, it sounds like you've made the program a bit complicated with vectors of classes of vectors etc.., so you'll have to go back and re-check things till you find out where you went wrong.

02-26-2005, 03:18 PM
I have been checking out cmesh like you said...and have found the problem :rolleyes:

I had to change this


to this...


I forgot that I need to take 1 from size() as it returns the number of elements not the actual index of the last one... :o
Thanks for your help :)