Click here to Skip to main content
15,904,023 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,

I'm fairly new to C++, and even newer to Computer Graphics, and I've been assigned a parser/loader for Wavefront OBJ files, but I kept getting an error in the file parser:

'Run-Time Check Failure #2 - Stack around the variable 'c' was corrupted.'

And here's my code:

void loader(char* fileName)
		{

		ifstream in;
		in.open(fileName, ifstream::in);
		if(!in)
		{
			cout << "Error:File Not Open";
			exit (1);
		}
		NormalNum = 0;
		TriangleNum = 0;
		VertexNum = 0;
		GLfloat x, y, z;
		GLubyte a, b, c;

		char str[512];

		while(in)
		{
			in.getline(str, 512);
			if (str[0]=='v' && str[1]=='n')
			{
				printf("1");
				sscanf_s(str,"vn %f %f %f", &x, &y, &z);
				printf("1.2");
				NormalArray.push_back(x);
				NormalArray.push_back(y);
				NormalArray.push_back(z);
				NormalNum++;
			}
			else
				if (str[0]=='v')
					{
						sscanf_s(str, "v %f %f %f",&x, &y, &z);
						VertexArray.push_back(x);
						VertexArray.push_back(y);
						VertexArray.push_back(z);
						VertexNum++;
					}
				else
				if (str[0]=='f')
					{	
						sscanf_s(str,"f %d %d %d", &a, &b, &c);
						IndexArray.push_back(a);
						IndexArray.push_back(b);
						IndexArray.push_back(c);
						TriangleNum++;
					}
			
				
		}
		in.close();
}


I've run some flags troughout the code, and I can say the code is stopping after the in.close(), I'm assuming it is getting some error in freeing the memory for the variable.

BTW, I'm using Visual Studio 2010, and I'm a computer sciences student, so I can take some jargon ;)

EDIT: This is coded inside a struct like this:

XML
typedef struct _Model{
    int VertexNum, NormalNum, TriangleNum;

    vector<GLfloat> VertexArray;
    vector<GLfloat> NormalArray;
    vector<GLubyte> IndexArray;

    void loader(char* fileName)



Thankz
Posted
Updated 27-Mar-12 10:42am
v2

1 solution

If GLubyte is what it looks like, an alias for byte, then your last sscanf_s is going to overwrite memory near a b and c. Do the sscanf_s into integer-sized temporary variables then cast to GLubyte as you are doing the push_backs.

Cheers,
Peter
 
Share this answer
 
Comments
Juuca 27-Mar-12 18:28pm    
A friend noticed it after I posted it xD but I've used a better solution, took advatage of the stream usage, and pass by the >> operator directly to the variables.

Thankz anyway ;)
Juca

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900