title: C++ Strange Access Violation with OpenGL
body: <p>I'm pretty new to C++ so I hope I can get some help here.
  I try to port my Game Engine to C++ but C++ behaves a litle bit... "Strange".
  Following Situation:</p>
  
  <p>if I run test1() It all works as it should.</p>
  
  <p><strong>main.cpp</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  
  #include "../headers/base.h"
  
  #include "../headers/DemoGame.h"
  #include "../headers/TestShader.h"
  
  using namespace std;
  using namespace engine;
  
  
  void run(TestShader* t, GLuint VAO, GLFWwindow* w)
  {
      glfwPollEvents();
  
  
      glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
      glClear(GL_COLOR_BUFFER_BIT);
  
      glUseProgram(t-&gt;progID);
      glBindVertexArray(VAO);
      glDrawArrays(GL_TRIANGLES, 0, 3);
      glBindVertexArray(0);
  
      glfwSwapBuffers(w);
  }
  
  void test1()
  {
      Window w = Window(800, 600, "test");
      TestShader t = TestShader();
      GLuint VAO, VBO;
  
      GLfloat vertices[9] = {
          -0.5f, -0.5f, 0.0f,
          0.5f, -0.5f, 0.0f,
          0.0f,  0.5f, 0.0f
      };
  
  
      glGenVertexArrays(1, &amp;VAO);
      glGenBuffers(1, &amp;VBO);
  
      glBindVertexArray(VAO);
  
      glBindBuffer(GL_ARRAY_BUFFER, VBO);
      glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  
      glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
      glEnableVertexAttribArray(0);
  
      glBindBuffer(GL_ARRAY_BUFFER, 0);
  
      glBindVertexArray(0);
  
      while (!glfwWindowShouldClose(w.getWindow()))
      {
          run(&amp;t, VAO, w.getWindow());
      }
  
  
  }
  
  void test2()
  {
      DemoGame game = DemoGame();
      game.start();
  }
  
  int main()
  {
  
      test1();
  
      return 0;
  }
  </code></pre>
  
  <p>If i'm running test2() with following involved classes:</p>
  
  <p><strong>Engine.h</strong></p>
  
  <pre><code>#pragma once
  
  #ifndef H_ENGINE
  #define H_ENGINE
  
  #include "base.h"
  
  namespace engine
  {
      class Engine
      {
          private:
              bool running;
  
          public:
              void start()
              {
                  init();
                  process();
              }
  
              void stop()
              {
                  this-&gt;running = false;
              }
  
          private:
              void process()
              {
                  update();
              }
  
          public:
              virtual void init() = 0;
              virtual void update() = 0;
              virtual void render() = 0;
              virtual void terminate() = 0;
      };
  }
  
  #endif
  </code></pre>
  
  <p><strong>DemoGame.h</strong></p>
  
  <pre><code>#pragma once
  
  #ifndef DEMO_DEMO_GAME
  #define DEMO_DEMO_GAME
  
  #include "base.h"
  
  #include "Window.h"
  #include "Engine.h"
  #include "TestShader.h"
  
  
  using namespace engine;
  
  class DemoGame : public Engine
  {
  
      public:
          Window* w;
          TestShader* t;
  
          GLuint VBO, VAO;
  
      public:
          DemoGame() : Engine() { }
  
      public:
          void init();
  
          void update();
  
          void render();
  
          void terminate();
  };
  
  #endif
  </code></pre>
  
  <p><strong>DemoGame.cpp</strong></p>
  
  <pre><code>#include "../headers/DemoGame.h"
  #include &lt;iostream&gt;
  
  using namespace std;
  
  void DemoGame::init()
  {
      cout &lt;&lt; "ping" &lt;&lt; endl;
  
      Window wi = Window(800, 600, "test");
      w = &amp;wi;
      TestShader te = TestShader();
      t = &amp;te;
  
  
  
      GLfloat vertices[9] = {
          -0.5f, -0.5f, 0.0f,
          0.5f, -0.5f, 0.0f,
          0.0f,  0.5f, 0.0f
      };
  
  
      glGenVertexArrays(1, &amp;VAO);
      glGenBuffers(1, &amp;VBO);
  
      glBindVertexArray(VAO);
  
      glBindBuffer(GL_ARRAY_BUFFER, VBO);
      glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  
      glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
      glEnableVertexAttribArray(0);
  
      glBindBuffer(GL_ARRAY_BUFFER, 0);
  
      glBindVertexArray(0);
  
      while (!glfwWindowShouldClose(w-&gt;getWindow()))
      {
          render();
      }
  }
  
  void DemoGame::update()
  {
  
  }
  
  void DemoGame::render()
  {
      glfwPollEvents();
  
  
      glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
      glClear(GL_COLOR_BUFFER_BIT);
  
      glUseProgram(t-&gt;progID);
      glBindVertexArray(VAO);
      glDrawArrays(GL_TRIANGLES, 0, 3);
      glBindVertexArray(0);
  
      glfwSwapBuffers(w-&gt;getWindow());
  }
  
  void DemoGame::terminate()
  {
  
  }
  </code></pre>
  
  <p>It works aswell. But as you may see Engine.h is supposed to control the mainloop. If I change the code a little bit:</p>
  
  <p><strong>Engine.h</strong></p>
  
  <pre><code>#pragma once
  
  #ifndef H_ENGINE
  #define H_ENGINE
  
  #include "base.h"
  
  namespace engine
  {
      class Engine
      {
          private:
              bool running;
  
          public:
              void start()
              {
                  init();
  
                  running = true;
  
                  while (running)
                  {
                      process();
                  }
  
              }
  
              void stop()
              {
                  this-&gt;running = false;
              }
  
          private:
              void process()
              {
                  update();
              }
  
          public:
              virtual void init() = 0;
              virtual void update() = 0;
              virtual void render() = 0;
              virtual void terminate() = 0;
      };
  }
  
  #endif
  </code></pre>
  
  <p><strong>DemoGame.cpp</strong></p>
  
  <pre><code>#include "../headers/DemoGame.h"
  #include &lt;iostream&gt;
  
  using namespace std;
  
  void DemoGame::init()
  {
      cout &lt;&lt; "ping" &lt;&lt; endl;
  
      Window wi = Window(800, 600, "test");
      w = &amp;wi;
      TestShader te = TestShader();
      t = &amp;te;
  
  
  
      GLfloat vertices[9] = {
          -0.5f, -0.5f, 0.0f,
          0.5f, -0.5f, 0.0f,
          0.0f,  0.5f, 0.0f
      };
  
  
      glGenVertexArrays(1, &amp;VAO);
      glGenBuffers(1, &amp;VBO);
  
      glBindVertexArray(VAO);
  
      glBindBuffer(GL_ARRAY_BUFFER, VBO);
      glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  
      glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
      glEnableVertexAttribArray(0);
  
      glBindBuffer(GL_ARRAY_BUFFER, 0);
  
      glBindVertexArray(0);
  
  }
  
  void DemoGame::update()
  {
      render();
  }
  
  void DemoGame::render()
  {
      glfwPollEvents();
  
  
      glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
      glClear(GL_COLOR_BUFFER_BIT);
  
      glUseProgram(t-&gt;progID);
      glBindVertexArray(VAO);
      glDrawArrays(GL_TRIANGLES, 0, 3);
      glBindVertexArray(0);
  
      glfwSwapBuffers(w-&gt;getWindow());
  }
  
  void DemoGame::terminate()
  {
  
  }
  </code></pre>
  
  <p>Now all of a sudden I get an "Access Violation". The question is why?
  The file "base.h" just contains</p>
  
  <pre><code>#define GLEW_STATIC
  #include "GL/glew.h"
  #include "GLFW/glfw3.h"
  </code></pre>
  
  <p>and the classes Window and TestShader shouldn't matter because they work in the first two examples. As I stated before I'm pretty new to C++ and I just don't understand why that doesn't work. Can you please help me finding out at least why that doesn't work or better help me solving the problem.</p>
  
  <p>This is my second attempt to get a useful answer from StackOverflow by posting a question. Please do me a favour. Please consider read the situation before you mark this question an duplicate. The last time it wasn't an duplicate the problem was by far different.</p>
  
  <p><strong>Edit</strong></p>
  
  <p>As requested the Error message(sry I'm at work so the language is german)</p>
  
  <blockquote>
    <p>Ausnahme ausgelöst bei 0x0126489D in GLFWGame.exe: 0xC0000005:
    Zugriffsverletzung beim Lesen an Position 0xCCCCCEA4.</p>
    
    <p>Falls ein Handler für diese Ausnahme vorhanden ist, kann das Programm
    möglicherweise weiterhin sicher ausgeführt werden.</p>
  </blockquote>
  
  <p>And I'll try to shorten the code to the most important.</p>