Memory exception in program - file handling and strings
Question
I'm getting the following error:
Unhandled exception at 0x7580b9bc in _ObjDraw.exe: Microsoft C++ exception: std::invalid_argument at memory location 0x0024f718..
When I run:
ifstream file(fileName);
if(!file)
{
return;
}
faces = vector<Face>();
string number = "";
Face tFace;
while(!file.eof())
{
getline(file, Buffer);
if(Buffer[0] == 'f')
{
//Faces have vertices, normals, and textures
size_t firstSpace = Buffer.find_first_of(' ', 0);
size_t firstSlash = Buffer.find_first_of('/', firstSpace + 1);
size_t nextSpace = Buffer.find_first_of(' ', firstSpace + 1);
size_t nextSlash = Buffer.find_first_of('/', firstSlash + 1);
if(firstSlash == string::npos) //Implies that there are no normals or textures
{
do
{
//Get the vertex number and add it to the face
nextSpace = Buffer.find_first_of(' ', firstSpace + 1);
number.assign(Buffer, firstSpace + 1, nextSpace - 1);
tFace.addVertex(stoi(number));
//Move to the next vertex
firstSpace = nextSpace;
}while(firstSpace != string::npos);
}
else //implies that there are textures and/or normals
{
if(nextSlash < nextSpace) //Normal's are present
{
do
{
//Get the vertex number
number.assign(Buffer, firstSpace + 1, firstSlash - 1);
tFace.addVertex(stoi(number));
//Get the normal
number.assign(Buffer, firstSlash + 1, nextSlash - 1);
if(number != "")
{
tFace.addTexture(stoi(number));
}
//Get the texture
number.assign(Buffer, nextSlash + 1, nextSpace - 1);
if(number != "")
{
tFace.addNormal(stoi(number));
}
//Get next positions
firstSpace = nextSpace;
firstSlash = Buffer.find_first_of('/', firstSpace);
nextSlash = Buffer.find_first_of('/', firstSlash + 1);
nextSpace = Buffer.find_first_of(' ', firstSpace + 1);
}while(firstSpace != string::npos);
}
else //Only textures
{
do
{
//Get the vertex number
number.assign(Buffer, firstSpace + 1, firstSlash - 1);
tFace.addTexture(stoi(number));
//Get the normal number
number.assign(Buffer, firstSlash + 1, nextSpace - 1);
tFace.addNormal(stoi(number));
firstSpace = nextSpace;
nextSpace = Buffer.find_first_of(' ', firstSpace + 1);
firstSlash = Buffer.find_first_of('/', firstSlash + 1);
}while(firstSpace != string::npos);
}
}
//Add tFace to face vector
faces.push_back(tFace);
tFace = Face();
}
}
file.close();
}
123.obj
has the following contents:
f 1//2 2//2 3//2 2//2
- This causes the exception
Having f 1 2 3 4
or f 1/2/3 2/3/4 3/4/5
works as it should. It's only when there is a double slash that a problem occurs.
When using the file with the '//', the first if statement should be false and the else
block should be executed, but for some reason, it ends up entering the first loop.
firstSlash
is 3 and nextSlash
is 4, when i output them to check.
Face and Vertex are classes, with the appropriate member functions
Solution
Just turn on first chance exception catching (in the menus Debug ==> Exceptions... or CTRL+ALT E in VS 2010) and run it under debugger. It will stop immediately when the exception is thrown and you can go up the stack to your code and (maybe) understand what is wrong.
OTHER TIPS
find_first_of
doesn't throw, neither does a while
or a comparison, so your error must be in one of the //code
segments.
You should paste more code if you want an answer
--- after the edit---
The code could use a lot more checks, you only check for firstSpace != npos
in your while condition, while nextSpace
and firstSlash
could easily be npos
too during the next iteration.