I'm trying to follow along with the gltut tutorials and for some reason when I call GLDrawArrays my program segmentation faults. I've been looking at the state of my application with the Mac OpenGL Profiler and Googling and I can't seem to figure out why it isn't working. I may have missed setting some OpenGL state somewhere.

My code is here, it segfaults on line 32. If I don't call glEnableClientState(GL_VERTEX_ARRAY); and the respective glDisableClientState(GL_VERTEX_ARRAY); it compiles and runs, but I get no rendering, which is what I would expect given my limited OpenGL knowledge.

  • \$\begingroup\$ do you link SFML as static or dynamic? the default build is for dynamic linking, when trying to link it as static, you'll get segmentation fault after exiting drawing functions, don't quote me on this, though, i'm no expert, i just had a similar issue and that was the problem \$\endgroup\$
    – dreta
    Feb 15, 2012 at 4:50
  • \$\begingroup\$ I link it dynamically. I can do immediate mode drawing just fine. \$\endgroup\$
    – sensae
    Feb 15, 2012 at 5:17
  • \$\begingroup\$ Is it because you're trying to draw a triangle with two vertices? \$\endgroup\$
    – user10968
    Feb 19, 2012 at 5:48
  • \$\begingroup\$ I guess I may have misunderstood how glDrawArrays works, but even if I try to draw two vertices it segfaults. I'll add that on my Mac it segfaults trying to read memory address 0x00000000. For some reason on my linux machine gdb can't get a backtrace, complaining Backtrace stopped: previous frame inner to this frame (corrupt stack?). \$\endgroup\$
    – sensae
    Feb 19, 2012 at 9:12
  • \$\begingroup\$ Do you mean "even if I try to draw three vertices it segfaults"? Triangles have three vertices :P \$\endgroup\$
    – user10968
    Feb 19, 2012 at 9:53

2 Answers 2


Well, there are a couple of things that are... odd about this code.


glDrawArrays(GL_TRIANGLES, 0, 2);

That will never actually draw anything. Triangles are made of 3 vertices and you told it to read 2.

I doubt that's causing the seg-fault, but it might be. Just change the 2 to 3.


const float vertexPositions[] = {
  0.75f, 0.75f, 0.0f, 0.2f,
  0.75f, -0.75f, 0.0f, 0.2f,
  -0.75f, -0.75f, 0.0f, 0.2f,

Again I'm not sure how you copied that wrong, but the "0.2f" for the W component should be "1.0f".

The source of your segfault:


Why do you call this? OK, I know why you're calling it. You've somehow taken a tutorial that's supposed to be showing you how to do modern, shader-based OpenGL, and are adapting it to fixed-function stuff.

If you are, then you need to adapt it properly.

glVertexAttribPointer is for setting up generic vertex attributes. You cannot use generic vertex attributes with fixed-function rendering. If you're going to do things in fixed-function, you need to actually do them in fixed function:

glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject);
glVertexPointer(4, GL_FLOAT, 0, 0);

glDrawArrays(GL_TRIANGLES, 0, 3);

glBindBuffer(GL_ARRAY_BUFFER, 0);
  • \$\begingroup\$ Great reply, I was operating with an incorrect understanding of the distinction between the different rendering methods. \$\endgroup\$
    – sensae
    Feb 19, 2012 at 21:28

My guess: You have 12 floats for your positions. You are telling to OpenGL that each vertex should be 4 floats by second parameter in here: glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);, so you have 3 vertices for these.

Then you tell OpenGL to draw 2 triangles in glDrawArrays command. But this would need 6 vertices - 3 vertices per triangle. So this could cause segmentation fault. Try to draw just one triangle or add more vertices.

  • \$\begingroup\$ Unless I'm reading it incorrectly, the OpenGL API states that the third argument to glDrawArrays is the number of points to read. Either way, calling GLDrawArrays(GL_TRIANGLES, 0, 1); results in the same error. \$\endgroup\$
    – sensae
    Feb 15, 2012 at 7:10
  • \$\begingroup\$ Maybe you can also check for OpenGL errors. Try to call opengl.org/sdk/docs/man/xhtml/glGetError.xml after each function call. If it doesn't return some error earlier. \$\endgroup\$
    – zacharmarz
    Feb 15, 2012 at 8:04
  • \$\begingroup\$ And also - is that fault in your first draw call or in some other? \$\endgroup\$
    – zacharmarz
    Feb 15, 2012 at 8:14
  • \$\begingroup\$ glGetError returns GL_NO_ERROR. I'm not sure what your second question was, but the issue happens when I call glDrawArrays. \$\endgroup\$
    – sensae
    Feb 15, 2012 at 18:14
  • \$\begingroup\$ Never mind. It was bad idea ;) \$\endgroup\$
    – zacharmarz
    Feb 15, 2012 at 19:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .