You are not logged in.

#1 2012-07-22 09:09:20

manasij7479
Member
Registered: 2011-10-20
Posts: 31

Is this a problem with the nvidia driver...or just my ignorance ?

Here is a little OpenGL program, that draws a triangle using index buffers.
(To OpenGL gurus.. spot anything wrong ?)

int main()
{
	sf::Window win(sf::VideoMode(400,400,32),"Manasij");
	GlewInit();
	mm::Program program
	(
		{
			mm::Shader(GL_VERTEX_SHADER,"vshader.vert"),
			mm::Shader(GL_FRAGMENT_SHADER,"fshader.frag")
		}
	);
	float vdata[]=
	{
		0.0f,0.0f,
		0.5f,0.0f,
		0.5f,0.5f,
		0.0f,0.5f
	};
	GLubyte indices[]={0,1,3};
	
	GLuint vao,vbo,ebo;
	glGenVertexArrays(1,&vao);
	glBindVertexArray(vao);
	glGenBuffers(1,&vbo);
	
	glBindBuffer(GL_ARRAY_BUFFER,vbo);
	glBufferData(GL_ARRAY_BUFFER,8*sizeof(float),vdata,GL_STATIC_DRAW);
	
	glGenBuffers(1,&ebo);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,ebo);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER,3*sizeof(GLubyte),indices,GL_STATIC_DRAW);
	
	glVertexAttribPointer(0,2,GL_FLOAT,GL_FALSE,0,0);
	glEnableVertexAttribArray(0);
	glBindAttribLocation(program.getHandle(),0,"pos");
	//glBindVertexArray(0);
	
	glUseProgram(program.getHandle());
	//glBindVertexArray(vao);
	glClearColor(1.0f,1.0f,1.0f,1.0f);
	
	while(win.isOpen())
	{
		sf::Event eve;
		while(win.pollEvent(eve))
			if(eve.type==sf::Event::Closed)
				win.close();
		glClear(GL_COLOR_BUFFER_BIT);
		
// 		glDrawArrays(GL_TRIANGLES,0,3);
		glDrawElements(GL_TRIANGLES,3,GL_UNSIGNED_BYTE,(GLvoid*)0);
		win.display();	
	}
	glDeleteBuffers(1,&vbo);
	glDeleteBuffers(1,&ebo);
	glDeleteVertexArrays(1,&vao);
	
	return 0;
}

The program 'works' fine, but whenever I close the window, thus exiting the loop, a wild segfault appears!
gdb says that:

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff557d164 in ?? () from /lib/libnvidia-glcore.so.302.17

Here is the complete code with a makefile: compile and test if you have sfml and glew installed (repo versions, nothing exotic !).
https://docs.google.com/open?id=0B0oubb … 0owXzhRTEU

Offline

#2 2012-07-22 19:00:24

cmtptr
Member
Registered: 2008-09-01
Posts: 135

Re: Is this a problem with the nvidia driver...or just my ignorance ?

You need to break the "while(win.isOpen())" loop after you win.close().  The segfault is occuring because you glDrawElements() one last time after you've already closed the window (and presumably destroyed the openGL context).

I'd suggest using gdb to attack bugs like this.  It can be extremely helpful:

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff557d164 in ?? () from /lib/libnvidia-glcore.so.302.17
(gdb) where
#0  0x00007ffff557d164 in ?? () from /lib/libnvidia-glcore.so.302.17
#1  0x00007ffff55841ef in ?? () from /lib/libnvidia-glcore.so.302.17
#2  0x00007ffff523247d in ?? () from /lib/libnvidia-glcore.so.302.17
#3  0x00000000004037be in main () at test.cpp:58
(gdb) frame 3
#3  0x00000000004037be in main () at test.cpp:58
58			glDrawElements(GL_TRIANGLES,3,GL_UNSIGNED_BYTE,(GLvoid*)0);
(gdb) list
53					win.close();
54				}
55			glClear(GL_COLOR_BUFFER_BIT);
56			
57	// 		glDrawArrays(GL_TRIANGLES,0,3);
58			glDrawElements(GL_TRIANGLES,3,GL_UNSIGNED_BYTE,(GLvoid*)0);
59			win.display();	
60		}
61	out:
62		glDeleteBuffers(1,&vbo);

Last edited by cmtptr (2012-07-22 19:04:43)

Offline

#3 2012-07-23 10:49:09

manasij7479
Member
Registered: 2011-10-20
Posts: 31

Re: Is this a problem with the nvidia driver...or just my ignorance ?

Thanks for taking the time to find out the problem.
Instead of putting a break , I just moved the event handling to the end of the loop.

I did the same with gdb, but missed the fact that the windows and context had ceased (it was still on the screen, though !) to exist before the last drawing call.

Offline

Board footer

Powered by FluxBB