title: GLWF capturing mouse leads to a segmentation fault
body: <p>For an opengl veteran this is probably easy, but I am completely new to opengl and I am currently working through a tutorial.</p>
  
  <p>I am now at the point where it is about to capture mouse events (to setup camera movement).</p>
  
  <p>However, although I am running the original code from the author, I am getting a segmentation fault.</p>
  
  <p>I guess he probably forget to set something additional up (just a guess). </p>
  
  <p>But, since I am an absolute opengl beginner, I am completely lost what this could be.</p>
  
  <p>The problem is that capturing the mouse movement yields a segmentation fault.</p>
  
  <p>The mouse and keyboard input is completely capsuled in a separate class called <code>Window.cpp</code>.</p>
  
  <p>I will post the complete code since I do not know where to start:</p>
  
  <p>Window.h:</p>
  
  <pre><code>#pragma once
  
  #include &lt;stdio.h&gt;
  
  #include &lt;/usr/local/Cellar/glew/2.1.0_1/include/GL/glew.h&gt;
  #include &lt;/usr/local/Cellar/glfw/3.3.2/include/GLFW/glfw3.h&gt;
  
  class Window
  {
      public:
          Window();
          Window(GLint windowWidth, GLint windowHeight);
  
          int initialize();
          GLint getBufferWidth() { return bufferWidth; }
          GLint getBufferHeight() { return bufferHeight; }
  
          bool getShouldClose() { return glfwWindowShouldClose(mainWindow); }
  
          bool* getKeys() { return keys; };
          GLfloat getXChange();
          GLfloat getYChange();
  
          void swapBuffer() { glfwSwapBuffers(mainWindow); }
  
          ~Window();
  
      private:
          GLFWwindow *mainWindow;
  
          GLint width, height;
          GLint bufferWidth, bufferHeight;
  
          bool keys[1024];
  
          GLfloat lastX, lastY, xChange, yChange;
          bool mouseFirstMoved;
  
          void createCallBacks();
          static void handleKeys(GLFWwindow* window, int key, int code, int action, int mode);
          static void handleMouse(GLFWwindow* window, double xPos, double yPos);
  };
  </code></pre>
  
  <p>And its implementation Window.cpp:</p>
  
  <pre><code>#include "Window.h"
  
  Window::Window() {
      width=800;
      height=600;
  
      xChange = 0.0f;
      yChange = 0.0f;
      mouseFirstMoved = false;
  
      for(size_t  i = 0; i &lt; 1024; ++i)
          keys[i] = 0;
  }
  
  Window::Window(GLint windowWidth, GLint windowHeight) {
      width = windowWidth;
      height = windowHeight;
  }
  
  int Window::initialize() {
      //Initialize GLFW
      if( !glfwInit() ) {
          printf("GLFW initialization failed");
          glfwTerminate();
          return 1;
      }
  
      //Setup GLFW window properties
      // OpenGL Version
      glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
      glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
      //Core profile means no backwards compatibility for OpenGL minor to 3
      glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
      //Make this code compatible for later version for OpenGL 3
      glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  
      mainWindow = glfwCreateWindow(width, height, "1st OpenGL Project Window", NULL, NULL);
      if(!mainWindow) {
          printf("GLFWwindow creation failed");
          glfwTerminate();
          return 1;
      }
  
      //Get buffer size information
      glfwGetFramebufferSize(mainWindow, &amp;bufferWidth, &amp;bufferHeight);
  
      //set context for GLEW to use
      glfwMakeContextCurrent(mainWindow);
  
      //handle key and mouse input
      createCallBacks();
      glfwSetInputMode(mainWindow, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
  
      //Allow modern extension features
      glewExperimental = GL_TRUE;
  
      if(glewInit() != GLEW_OK) {
          printf("GLEW initilization failed");
          glfwDestroyWindow(mainWindow);
          glfwTerminate();
          return 1;
      }
  
      //Video 15: enable depth buffer to order the triangles of the pyramid
      glEnable(GL_DEPTH_TEST);
  
      //Set viewport size
      glViewport(0, 0, bufferWidth, bufferHeight);
  
      glfwSetWindowUserPointer(mainWindow, this);
      return 0;
  }
  
  
  void Window::createCallBacks() {
      glfwSetKeyCallback(mainWindow, handleKeys);
      glfwSetCursorPosCallback(mainWindow, handleMouse);
  }
  
  
  GLfloat Window::getXChange() {
      GLfloat theChange = xChange;
      xChange = 0.0f;
      return theChange;
  }
  
  GLfloat Window::getYChange() {
      GLfloat theChange = yChange;
      yChange = 0.0f;
      return theChange;
  }
  
  void Window::handleKeys(GLFWwindow* window, int key, int code, int action, int mode) {
      Window* theWindow = static_cast&lt;Window*&gt; (glfwGetWindowUserPointer(window));
      if( key == GLFW_KEY_ESCAPE &amp;&amp; action == GLFW_PRESS) {
          glfwSetWindowShouldClose(window, GL_TRUE);
      }
  
  
      if( 0&lt;= key &amp;&amp; key &lt; 1024 ) {
          if(action == GLFW_PRESS) {
              theWindow-&gt;keys[key] = true;
          } else if(action == GLFW_RELEASE) {
              theWindow-&gt;keys[key] = false;
          }
      }
  
  }
  
  void Window::handleMouse(GLFWwindow* window, double xPos, double yPos) {
      Window* theWindow = static_cast&lt;Window*&gt;(glfwGetWindowUserPointer(window));
  
      if (theWindow-&gt;mouseFirstMoved)
      {
          theWindow-&gt;lastX = xPos;
          theWindow-&gt;lastY = yPos;
          theWindow-&gt;mouseFirstMoved = false;
      }
  
      theWindow-&gt;xChange = xPos - theWindow-&gt;lastX;
      theWindow-&gt;yChange = theWindow-&gt;lastY - yPos;
  
      theWindow-&gt;lastX = xPos;
      theWindow-&gt;lastY = yPos;
  }
  
  
  Window::~Window() {
      glfwDestroyWindow(mainWindow);
      glfwTerminate();
  }
  </code></pre>
  
  <p>The window loads and opens.</p>
  
  <p>But as soon as I move the mouse only slightly the program crashes and I get a segmentation fault.</p>
  
  <p>This is the output:</p>
  
  <pre><code>Segmentation fault: 11
  </code></pre>
  
  <p>However, pressing the ESCAPE button closes the window correctly as you would expect from this code.</p>
  
  <p>I am lost so any advise is highly appreciated.</p>