title: Qt 5.5 and OpenGL: read access violation exception raised when trying to call paintGL()
body: <p>I am writing a simple application that uses Qt 5.5 and OpenGL to draw some very basic geometry in a QtOpenGLWidget. Initially, much of the vertex data initialization and rendering code was taken out of a tutorial designed to draw a single triangle. This version worked correctly, but my own attempt at modifying it to facilitate 4 vertices to form a square has underwent without success. The program compiles with no errors, but crashes when executed without even drawing the ui to the screen, with the following error:</p>
  
  <pre><code>error: Exception at 0x53fe35, code: 0xc0000005: read access violation at: 0x0, flags=0x0 (first chance)
  </code></pre>
  
  <p>I am using Qt's OpenGL wrappers everywhere I can.</p>
  
  <p>The first potential (and probable) solution I found is in <a href="https://devtalk.nvidia.com/default/topic/720651/access-violation-in-nvoglv32-dll-how-do-i-track-down-the-problem-/" rel="nofollow">this</a> post; the problem in question has an identical error signature, and the description of the cause of the problem, that being incorrectly enabled or configured VAO's, also seems considerable to my case, considering the addition of another vertex to the VBO and my likely ineptitude to correctly reconfigure the glVertexAttribPointer. I cannot, however, find the problem in my code.</p>
  
  <p>GLWidget's (the OpenGL widget in question) declaration:</p>
  
  <pre><code>class GLWidget : public QOpenGLWidget, protected QOpenGLFunctions {
      Q_OBJECT
  
      void prepareVertexBuffers();
  
      void prepareVertexShader();
      void prepareFragmentShader();
      void prepareShaderProgram();
  
      void applyShaderPrograms();
  
      QOpenGLBuffer *vertBuffer;
      QOpenGLBuffer *elementBuffer;
      QOpenGLVertexArrayObject *vao;
  
      QOpenGLShader *vertShader;
      QOpenGLShader *fragShader;
      QOpenGLShaderProgram *shaderProgram;
  
  public:
      GLWidget(QWidget *parent = 0);
  
      void initializeGL();
      void resizeGL();
      void paintGL();
  
  signals:
      void shaderCompiled(const QString compilationLog);
  
  public slots:
      void updateFragmentShader(QString newsource);
  };
  </code></pre>
  
  <p>prepareVertexBuffers() function - here I define 4 vertices and arrange them into 2 triangles to form a square.</p>
  
  <pre><code>void GLWidget::prepareVertexBuffers() {
  
      static const GLfloat vertData[] = {
         -1.0f, 1.0f, 0.0f,
         -1.0f, -1.0f, 0.0f,
         1.0f,  -1.0f, 0.0f,
         1.0f,  1.0f, 0.0f,
      };
  
      static const GLushort elementData[] = {1, 2, 3, 2, 3, 4};
  
      vao = new QOpenGLVertexArrayObject(this);
      vao-&gt;create();
      vao-&gt;bind();
  
      vertBuffer = new QOpenGLBuffer(QOpenGLBuffer::VertexBuffer);
      vertBuffer-&gt;create();
      vertBuffer-&gt;allocate(vertData, 4 * 3 * sizeof(GLfloat) );
      vertBuffer-&gt;setUsagePattern( QOpenGLBuffer::StaticDraw );
      vertBuffer-&gt;bind();
  
      elementBuffer = new QOpenGLBuffer(QOpenGLBuffer::IndexBuffer);
      elementBuffer-&gt;create();
      elementBuffer-&gt;allocate(elementData, 6 * sizeof(GLushort) );
      elementBuffer-&gt;setUsagePattern( QOpenGLBuffer::StaticRead );
      elementBuffer-&gt;bind();
  }
  </code></pre>
  
  <p>paintGL(), where I suspect the problem is:</p>
  
  <pre><code>void GLWidget::paintGL() {
      vao-&gt;bind();
      vertBuffer-&gt;bind();
      elementBuffer-&gt;bind();
  
      glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 3, (void*)0 );
      glEnableVertexAttribArray(0);
  
      glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
      glClear(GL_COLOR_BUFFER_BIT);
  
      glDrawArrays(GL_TRIANGLES, 0, 4);
      glDisableVertexAttribArray(0);
      elementBuffer-&gt;release();
      vertBuffer-&gt;release();
      vao-&gt;release();
  }
  </code></pre>
  
  <p>It is obvious to note that I have virtually no experience in using OpenGL, and so clues as to how to structure my code correctly and general coding practices would also be appreciated.</p>