Sign in to follow this  

intermittent seg fault : during geometry rendering

This topic is 3708 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi Guys I have this weird problem of getting seg faults intermittently. I'm sure I'm doing something wrong during memory allocation or of that sort. But, can't figure out what.. Here's my code for setting up a NXN grid of triangles for possible terrain rendering:
void drawHmGrid()
{
  // define a NXN grid
	static const int gridX = 16;
	static const int gridY = 16;

	
	Mathbox *m = new Mathbox();			// new mathbox class
	int numfaces = (gridX-1) * (gridY-1) * 2;	// number of faces
	int numverts = 3;				// number of vertices per face
	int numcoords = 3;				// number of co-ordinates per vertex
	vec3 v[gridX*gridY];                            // vertices
        int index = 0;                                  // index
	int cnt =0;					// array counter	

	GLubyte order1[numfaces*numverts];
	int ct = 0;
	int counter = 1;

	for(int ix1=0; ix1 < (numfaces/2) * numverts; ix1+=3)
		{
		order1[ix1] = ct;
		//cout << ct << ", ";
		order1[ix1+1] = ct+1;
		//cout << ct+1 << ", ";
		order1[ix1+2] = ct+(gridY+1);
		//cout << ct+5 << endl;
		ct++;
		counter++;
		if(counter == gridY) {ct++; counter=1;}
		}
	
	//cout << "final ct= " << ct << endl;
	ct = ct + (gridY - 1);
	counter = 1;
	
	for(int ix1=(numfaces/2) * numverts; ix1 < numfaces * numverts; ix1+=3)
		{
		order1[ix1] = ct;
	        //cout << ct << ", ";
		order1[ix1+1] = ct-1;
		//cout << ct-1 << ", ";
		order1[ix1+2] = ct-(gridY+1);
		//cout << ct-5 << endl;
		ct--;
		counter++; 
		if(counter == gridY) {ct--; counter=1;}
		}

	//cout << "done." << endl;

	
	float *vtx = new float[numfaces * numverts * numcoords];//vertex array
	float *norm = new float[numfaces * numverts * numcoords];//normal array
	float *tex = new float[numfaces * numverts * numcoords];//texture array
	int vc = 0;
	
	for (int x=0; x<gridX; x++)				//height = 0 for testing geometry
		for(int y=0; y<gridY; y++)
		{
			v[index] = m->setVec3(x, y, findHeight(x, y));
			//v[index] = m->setVec3(x, y, 0.0);
			//cout << index << ". ";
			//m->printVec3(v[index]);
			index++;
		}


	for (int idx1 = 0; idx1 < numfaces; idx1++)
	{
		for (int idx2 = 0; idx2 < numverts; idx2++)
		{
			 vc = ( idx1 * numverts ) + idx2;
			for(int idx3 = 0; idx3 < numcoords; idx3++)
				{
					cnt = ( ((idx1 * numverts) + idx2) * numcoords );
					vtx[cnt] = v[ vc ].x;
					norm[cnt] = 0.0;
					cnt++;
					vtx[cnt] = v[ vc ].y;
					norm[cnt] = 0.0;
					cnt++;
					vtx[cnt] = v[ vc ].z;
					norm[cnt] = -1.0;
					cnt =0;
				}
		}	
	}

	// draw arrays
	glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
	
                //rendering the cube
                glEnableClientState(GL_VERTEX_ARRAY);
                glVertexPointer(3, GL_FLOAT, 0, &(vtx[0]));

                glEnableClientState(GL_NORMAL_ARRAY);
                glNormalPointer(GL_FLOAT, 0, &(norm[0]));

	glDrawElements(GL_TRIANGLES, numfaces * numcoords, GL_UNSIGNED_BYTE, order1);

		glDisableClientState(GL_VERTEX_ARRAY);
                //glDisableClientState(GL_TEXTURE_COORD_ARRAY);
                glDisableClientState(GL_NORMAL_ARRAY);


	//drawArrays(vtx, tex, norm, true, false, true, numfaces, numverts);
	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
	
	delete(vtx);
	delete(tex);
	delete(norm);
	delete(m);	
}

It works sometimes, then annoyingly so it seg faults sometimes. If I just rerun the program (yes, without any change) its fine, seamless. I added the delete statements as I thought it might be because I didn't destroy the structures that I created. But, still no luck! [Edited by - rrarunan on October 14, 2007 9:11:54 AM]

Share this post


Link to post
Share on other sites
If the program behaves differently between runs with the same inputs then it is exhibiting undefined behaviour. There are several cases for this, but perhaps the most common is uninitialised memory.

Your debugger will be far more helpful in fixing this problem than anybody reading your code. Find out at which line the access violation occurs and work backwards. Make sure you're using a debug build for the debugging process, or you'll probably do more harm than good.

If you're still stuck, tell us what compiler and settings you're using, which line the fault occurs at, which type of seg-fault it is (read, write or execute) and the linear address of the inaccessible memory.

Also, it's much easier on the eyes if you put your code inside [source] tags.

Admiral

Share this post


Link to post
Share on other sites
Try running your program through a memory profiler like valgrind. You also might want to try a debug allocator, possibly something like duma (http://duma.sourceforge.net/). You should also configure your system to write core files on the crashes. Doing these things will make it a lot easier to track down the memory corruption issue.

*Edit*

I also thought I'd add that in the past, I've seen unpredictable segfaults in glDrawElements when the parameters are wrong or if the opengl state is not set up properly before making the call.

Share this post


Link to post
Share on other sites
stick all these just before u call glDrawElements
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
etc (color array texcoords etc) check the opengl spec for a full list

now it should work (but u wont see anything :) )
thus reenable one at a time + run the program (u will find out which one is causing the problem)

also draw elements with GL_UNSIGNED_BYTE seems wierd thats only 256 elemetns ( i assume u have more than that)
also for models u will want to share vertices if theyre in the same place, ie for each face dont create 3 seperate vertices

Share this post


Link to post
Share on other sites
	float *vtx = new float[numfaces * numverts * numcoords];//vertex array
float *norm = new float[numfaces * numverts * numcoords];//normal array
float *tex = new float[numfaces * numverts * numcoords];//texture array

delete(vtx);
delete(tex);
delete(norm);



You're creating arrays with the new operator, so you need to delete them with:

delete [] vtx;
delete [] tex;
delete [] norm;




This will cause memory leaks.

[Edited by - venzon on October 17, 2007 11:34:17 AM]

Share this post


Link to post
Share on other sites

This topic is 3708 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this