title: Segmentation Fault when accessing (non-pointer) member variable in Qt class instance
body: <p>I am having some issues with a segmentation fault being caused by access
  to a member variable in a Qt application.  My experience of segmentation
  faults have shown them generally to be a result of accessing a memory 
  location that is out of bounds.  However, I'm struggling to figure out 
  why this would be the case in my code.</p>
  
  <p>The class in question is a subclass of QFrame and contains a number of 
  member variables, including one originally called <code>m_Zoom</code> of type 
  <code>unsigned int</code>.  When the frame is created, a number of functions are called 
  that cause the zoom level to be set and then the window is drawn.  This 
  all works fine.  There are then a number of ways that the zoom level can 
  be changed: opening a new file or pressing either + or -.  These read 
  the existing zoom level and then decide whether to change it.  Simply 
  reading the existing zoom level causes a segmentation fault.</p>
  
  <p>Note that this variable is not a pointer or in an array or anything 
  unusual.  The definition was along the lines of:</p>
  
  <pre><code>class MyClass : public QFrame
  {
      Q_OBJECT
  
  public:
      MyClass(QWidget *parent = 0);
      void SetZoomLevel(unsigned int zoom);
      void ZoomIn();
  
  protected:
      void mousePressEvent(QMouseEvent *event);
      void mouseReleaseEvent(QMouseEvent *event);
      void mouseMoveEvent(QMouseEvent *event);
      void paintEvent(QPaintEvent *event);
  
  private:
      // A number of private functions for performing calculations
  
      // Some variables including:
      double m_OriginShift;
      unsigned int m_Zoom;
      // Some more variables
  };
  </code></pre>
  
  <p>The function <code>paintEvent</code> and all the <code>mouse*</code> event functions access 
  <code>m_Zoom</code> and these work fine (the mouse is used to move the view 
  around).  The constructor calls <code>SetZoomLevel</code> (which is basically 
  <code>m_Zoom = zoom</code>).  Again, this works with no problems.  <code>ZoomIn</code> starts 
  with:</p>
  
  <pre><code>void MyClass::ZoomIn()
  {
      qDebug() &lt;&lt; "Zoom In";
      unsigned int existingZoom = m_Zoom; // [1]
      qDebug() &lt;&lt; "Existing zoom is " &lt;&lt; existingZoom;
  
      ...
  </code></pre>
  
  <p>The segmentation fault happens in this function, before the second 
  <code>qDebug</code> reports anything.</p>
  
  <p>I've tried stepping through the application in cygwin's gdb and I get to 
  the line marked <code>[1]</code> and type <code>stepi</code> and get an immediate segfault.</p>
  
  <p>Having moved the variable around (in case something else was overwriting 
  it's location), changed to using a pointer with <code>malloc</code>d memory, moved 
  it into a separate structure, moved it into a separate class (both with a member instance and with a member pointer with a <code>new</code> line in the constructor) and 
  probably a few other random and unsuccessful acts of desperation, I've 
  completely run out of ideas.</p>
  
  <p>Please can anyone suggest any good ways that I could go about debugging 
  this issue?</p>
  
  <p>I'm using Qt 4.6.3 on Windows XP compiling with GNU Make 3.81 and g++ 
  version 4.4.0 (from the mingw that came with the Qt installer).</p>