title: OpenGL -- glGenVertexArrays, "thread 1: exc_bad_access (code =1, address=0x0)"
body: <p>I am writing a <strong>openGL program(C++)</strong> which draws a ground with two 3D objects above it. The programming tool I use is <strong>Xcode version 8.0(8A218a)</strong>(OSX 10.11.6).</p>
  
  <p><strong><em>my code(main.cpp):</em></strong></p>
  
  <pre><code>   #include &lt;GL/glew.h&gt;
      #include &lt;GL/freeglut.h&gt;
      #include &lt;glm/glm.hpp&gt;
      #include &lt;glm/gtc/matrix_transform.hpp&gt;
      #include &lt;iostream&gt;
      #include &lt;fstream&gt;
      using namespace std;
  
      using glm::vec3;
      using glm::mat4;
  
      GLint programID;
      //initialize all OpenGL objects
      GLuint groundVAO, groundVBO, groundEBO; //ground
  
  
      bool checkStatus( //OK
          GLuint objectID,
          PFNGLGETSHADERIVPROC objectPropertyGetterFunc,
          PFNGLGETSHADERINFOLOGPROC getInfoLogFunc,
          GLenum statusType)
          {
          GLint status;
          objectPropertyGetterFunc(objectID, statusType, &amp;status);
          if (status != GL_TRUE)
          {
              GLint infoLogLength;
              objectPropertyGetterFunc(objectID, GL_INFO_LOG_LENGTH, &amp;infoLogLength);
              GLchar* buffer = new GLchar[infoLogLength];
  
              GLsizei bufferSize;
              getInfoLogFunc(objectID, infoLogLength, &amp;bufferSize, buffer);
              cout &lt;&lt; buffer &lt;&lt; endl;
  
              delete[] buffer;
              return false;
          }
          return true;
      }
  
       bool checkShaderStatus(GLuint shaderID) //OK
      {
          return checkStatus(shaderID, glGetShaderiv, glGetShaderInfoLog, GL_COMPILE_STATUS);
      }
  
       bool checkProgramStatus(GLuint programID) //OK
      {
          return checkStatus(programID, glGetProgramiv, glGetProgramInfoLog, GL_LINK_STATUS);
      }
  
      string readShaderCode(const char* fileName) //OK
      {
          ifstream meInput(fileName);
          if (!meInput.good())
          {
              cout &lt;&lt; "File failed to load..." &lt;&lt; fileName;
              exit(1);
          }
          return std::string(
              std::istreambuf_iterator&lt;char&gt;(meInput),
              std::istreambuf_iterator&lt;char&gt;()
          );
      }
  
      void installShaders() //OK
      {
          GLuint vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
          GLuint fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
  
          const GLchar* adapter[1];
          //adapter[0] = vertexShaderCode;
          string temp = readShaderCode("VertexShaderCode.glsl");
          adapter[0] = temp.c_str();
          glShaderSource(vertexShaderID, 1, adapter, 0);
          //adapter[0] = fragmentShaderCode;
          temp = readShaderCode("FragmentShaderCode.glsl");
          adapter[0] = temp.c_str();
          glShaderSource(fragmentShaderID, 1, adapter, 0);
  
          glCompileShader(vertexShaderID);
          glCompileShader(fragmentShaderID);
  
          if (!checkShaderStatus(vertexShaderID) || 
      !checkShaderStatus(fragmentShaderID))
              return;
  
          programID = glCreateProgram();
          glAttachShader(programID, vertexShaderID);
          glAttachShader(programID, fragmentShaderID);
          glLinkProgram(programID);
  
          if (!checkProgramStatus(programID))
              return;
  
          glDeleteShader(vertexShaderID);
          glDeleteShader(fragmentShaderID);
  
          glUseProgram(programID);
       }
  
       void keyboard(unsigned char key, int x, int y)
       {
           //TODO:
       }
  
       void sendDataToOpenGL()
       {
           //TODO:
          //create solid objects here and bind to VAO &amp; VBO
  
          //Ground, vertices info
          const GLfloat Ground[]
          {
               -5.0f, +0.0f, -5.0f, //0
               +0.498f, +0.898, +0.0f, //grass color
               +5.0f, +0.0f, -5.0f, //1
               +0.498f, +0.898, +0.0f,
               +5.0f, +0.0f, +5.0f, //2
               +0.498f, +0.898, +0.0f,
               -5.0f, +0.0f, +5.0f
          };
          GLushort groundIndex[] = {1,2,3, 1,0,3};
  
          //Pass ground to vertexShader
          //VAO
          glGenVertexArrays(1, &amp;groundVAO);
          glBindVertexArray(groundVAO);
  
          //VBO
          glGenBuffers(1, &amp;groundVBO);
          glBindBuffer(GL_ARRAY_BUFFER, groundVBO);
          glBufferData(GL_ARRAY_BUFFER, sizeof(Ground), Ground, GL_STATIC_DRAW);
  
          //EBO
          glGenBuffers(1, &amp;groundEBO);
          glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, groundEBO);
          glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(groundIndex), groundIndex, GL_STATIC_DRAW);
  
          //connectToVertexShader
          glEnableVertexAttribArray(0); //position
          glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 6, 0);
          glEnableVertexAttribArray(1); //color
          glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 6, (char*)(sizeof(float)*3));
       }
  
      void paintGL(void)
      {
          //TODO:
          //render your objects and control the transformation here
          glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  
          //translate model
          glm::mat4 modelTransformMatrix = glm::translate(glm::mat4(), vec3(+0.0f, +0.0f, -3.0f));
          //perspective view
          glm::mat4 projectionMatrix = glm::perspective(+40.0f, +1.0f, +1.0f, +60.0f);
          //ultimate matrix
          glm::mat4 ultimateMatrix;
  
          //register location on the graphics cards
          GLint ultimateMatrixUniformLocation = glGetUniformLocation(programID, "ultimateMatrix");
          /*GLint modelTransformMatrixUniformLocation = glGetUniformLocation(programID, "modelTransformMatrix");
          GLint projectionMatrixUniformLocation = glGetUniformLocation(programID, "projectionMatrix");*/
  
          //drawing the ground
  
          /*glUniformMatrix4fv(modelTransformMatrixUniformLocation, 1, GL_FALSE, &amp;modelTransformMatrix[0][0]);
          glUniformMatrix4fv(projectionMatrixUniformLocation, 1, GL_FALSE, &amp;projectionMatrix[0][0]);*/
          glBindVertexArray(groundVAO);
          ultimateMatrix = projectionMatrix * modelTransformMatrix;
          glUniformMatrix4fv(ultimateMatrixUniformLocation, 1, GL_FALSE, &amp;ultimateMatrix[0][0]);
          glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);
  
          glFlush();
          glutPostRedisplay();
      }
  
      void initializedGL(void) //run only once
      {
          glewInit();
          glEnable(GL_DEPTH_TEST);
          sendDataToOpenGL();
          installShaders();
      }
  
      int main(int argc, char *argv[])
      {
          /*Initialization*/
          glutInit(&amp;argc, argv);
          glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
          glutCreateWindow("Try");
          glutInitWindowSize(700, 700);
  
          //const GLubyte* glversion = glGetString(GL_VERSION);
  
          /*Register different CALLBACK function for GLUT to response
          with different events, e.g. window sizing, mouse click or
          keyboard stroke */
          initializedGL();
  
          //glewExperimental = GL_TRUE;
  
          glutDisplayFunc(paintGL);
          glutKeyboardFunc(keyboard);
  
          /*Enter the GLUT event processing loop which never returns.
          it will call different registered CALLBACK according
          to different events. */
          //printf("OpenGL ver: %s\n", glversion);
          glutMainLoop();
  
          return 0;
      }
  </code></pre>
  
  <p><strong><em>VertexShaderCode.glsl:</em></strong></p>
  
  <pre><code>#version 430  // GLSL version your computer supports
  
  in layout(location=0) vec3 position;
  in layout(location=1) vec3 vertexColor;
  
  uniform mat4 ultimateMatrix;
  
  out vec3 theColor;
  
  void main()
  {
      vec4 v = vec4(position, 1.0);
      gl_Position = ultimateMatrix * v;
      theColor = vertexColor;
  } 
  </code></pre>
  
  <p><strong><em>FragmentShaderCode.glsl:</em></strong></p>
  
  <pre><code>#version 430 //GLSL version your computer supports
  
  out vec4 theColor2;
  in vec3 theColor;
  
  void main()
  {
      theColor2 = vec4(theColor, 1.0);
  }
  </code></pre>
  
  <p>Functions: <strong><em>checkStatus, checkShaderStatus, checkProgramStatus, readShaderCode, installShaders</em></strong> should be all fine.</p>
  
  <p><strong><em>void keyboard()</em></strong> can be ignored since I havent implemented it yet(just for keyboard control).</p>
  
  <p>I implemented the object "Ground" in sendDataToOpenGL(). But when I compiled and ran the program, <strong><em>"thread 1: exc_bad_access (code =1, address=0x0)"</em></strong> occured in the line of <strong><em>VAO</em></strong>:<br/>
  <a href="https://i.stack.imgur.com/WJ9Bp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WJ9Bp.png" alt="enter image description here"></a></p>
  
  <p>And the pop-out window is just a white screen instead of a green grass(3d).<br/>
  <a href="https://i.stack.imgur.com/bvwi6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bvwi6.png" alt="enter image description here"></a></p>
  
  <p>I have tried a method which was provided in other stackoverflow post: using <code>glewExperimental = GL_TRUE;</code>. I didnt see any errors by using that, but the popout screen vanished immediately just after it appeared. It seems that it couldnt help the problem.</p>
  
  <p>Can someone give me a help? Thank you! </p>