Advertisement Jump to content
Sign in to follow this  

OpenGL glDrawArrays causes Segmantation fault

This topic is 1659 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

Hello everyone,

My question may seem pretty newbish for you but I just can't figure it out and it's really bothering me.

So... Having little knowledge about OpenGL I wanted to find myself a tutorial and stumbled upon video series on youtube that was published about half a year ago (so it's fairly recent) and decided to just follow it. I am quite sure that the mentioned tutorial does not have the error I experience (it is shown that it works).

I am using Code::Blocks and the system is Arch Linux x64, the computer has two video cards: 1) integrated Intel graphics card 2) nvidia Geforce GT 620M.

There is this class Mesh in the tutorial



#include "mesh.h"
#include <vector>

Mesh::Mesh(Vertex* vertices, unsigned int numVertices){
  m_drawCount = numVertices;
  glGenVertexArrays(1,& m_vertexArrayObject);
  std::vector<glm::vec3> positions;
  std::vector<glm::vec2> texCoords;

  for(unsigned int i=0; i<numVertices;i++){

  glGenBuffers(NUM_BUFFERS, m_vertexArrayBuffers);

  glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffers[POSITION_VB]);
  glBufferData(GL_ARRAY_BUFFER,numVertices*sizeof(positions[0]), &positions[0],GL_STATIC_DRAW);
  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

  glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffers[TEXCOORD_VB]);
  glBufferData(GL_ARRAY_BUFFER,numVertices*sizeof(texCoords[0]), &texCoords[0],GL_STATIC_DRAW);
  glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);



  glDeleteVertexArrays(1,& m_vertexArrayObject);

void Mesh::Draw(){
  glDrawArrays(GL_TRIANGLES, 0, m_drawCount);

And I use it in my main.cpp

#include <iostream>
#include <GL/glew.h>
#include "display.h"
#include "shader.h"
#include "mesh.h"
#include "texture.h"
#include "glm/glm.hpp"
int main(){
  Display display(800, 600, "OpenGL");
  Vertex vertices[] = { Vertex(glm::vec3(-0.5, -0.5, 0), glm::vec2(0.0,0.0)),
			Vertex(glm::vec3(0, 0.5, 0), glm::vec2(0.5,1.0)),
			Vertex(glm::vec3(0.5, -0.5, 0), glm::vec2(1.0,0.0)),};
  Mesh mesh(vertices, sizeof(vertices)/sizeof(vertices[0]));
  Shader shader("./res/basicShader");
  Texture texture("./res/bricks.jpg");
  return 0;

But when I run my app in the debug mode it shows me this



Building to ensure sources are up-to-date
Selecting target:
Adding source dir: /home/lee/OpenGL/
Adding source dir: /home/lee/OpenGL/
Adding file: /home/lee/OpenGL/bin/Debug/OpenGL
Changing directory to: /home/lee/OpenGL/.
Set variable: LD_LIBRARY_PATH=.:
Starting debugger: /usr/bin/gdb -nx -fullname -quiet -args /home/lee/OpenGL/bin/Debug/OpenGL
Registered new type: wxString
Registered new type: STL String
Registered new type: STL Vector
Setting breakpoints
Debugger name and version: GNU gdb (GDB) 7.7.1
Child process PID: 6602
Program received signal SIGSEGV, Segmentation fault.
In __memcpy_sse2_unaligned () (/usr/lib/
#7 0x0000000000403386 in Mesh::Draw (this=0x7fffffffe700) at /home/lee/OpenGL/mesh.cpp:42
At /home/lee/OpenGL/mesh.cpp:42
#7 0x0000000000403386 in Mesh::Draw (this=0x7fffffffe700) at /home/lee/OpenGL/mesh.cpp:42


The line 42 is glDrawArrays(GL_TRIANGLES, 0, m_drawCount); from mesh.cpp


It is really confusing because ,as I've said, I just typed the whole code as it was done in the tutorial. Unfortunately I can not download the source code because it is not available for this part.


Can it be somehow connected with my hardware specifications?


I apologize beforehand if the answer is too simple and/or obvious. 

Edited by LeeBrowm

Share this post

Link to post
Share on other sites

When you are binding the texture coordinates

glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);

You are specifying index 0 in the first parameter. It looks like that should be index 1.

Edited by HappyCoder

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!