程序中的内存异常 - 文件处理和字符串
我收到以下错误: _ObjDraw.exe 中 0x7580b9bc 处未处理的异常:Microsoft C++ 异常:内存位置 0x0024f718.. 处的 std::invalid_argument..
当我运行时:
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
具有以下内容: f 1//2 2//2 3//2 2//2
- 这会导致异常 使用 f 1 2 3 4
或 f 1/2/3 2/3/4 3/4/5
可以正常工作。只有当存在双斜杠时才会出现问题。 当使用带有“//”的文件时,第一个 if 语句应该为 false,并且应该执行 else
块,但由于某种原因,它最终进入了第一个循环。 当我输出它们进行检查时,firstSlash
是 3,nextSlash
是 4。 Face 和 Vertex 是类,具有适当的成员函数
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需打开第一次机会异常捕获(在菜单 Debug ==> Exceptions... 或 VS 2010 中的 CTRL+ALT E 中)并在调试器下运行它。当抛出异常时,它将立即停止,您可以在堆栈中查找代码并(也许)了解问题所在。
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.
find_first_of
不会抛出异常,while
或比较也不会抛出异常,因此您的错误必须位于//code
段之一中。如果您想要答案,您应该粘贴更多代码
--- 在编辑后 ---
代码可以使用更多检查,您只在 while 条件中检查
firstSpace != npos
,而 < code>nextSpace 和firstSlash
在下一次迭代中也很容易成为npos
。find_first_of
doesn't throw, neither does awhile
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, whilenextSpace
andfirstSlash
could easily benpos
too during the next iteration.