title: Unusual behavior in c++ program, crashing often
body: <p><strong>I've recently hit an issue in my program and it seems as if nobody has had this problem either, I've scoured the internet for a solution but I just couldn't find it, let me explain.</strong></p>
  
  <h1>The issue</h1>
  
  <p>I've been working on a 2D platforming game for a while now and am slowly implementing new features, however, I've it hit a snag. I'm currently programming a AABB tree implementation into my game for collision and physics purposes and it seems to be causing lots of trouble. I eventually fixed all the errors in the program and got a working implementation, at least, I thought I did until random errors started popping up. To my surprise, it wasn't actually my AABB class at all, it was this single block of code.</p>
  
  <pre><code>while (fs.tellg() &lt; Size) {
      unsigned short int x, y, w, h;
      fs.read(reinterpret_cast&lt;char *&gt;(&amp;x), 2);//I'm reading from a binary
      fs.read(reinterpret_cast&lt;char *&gt;(&amp;y), 2);//file containing the AABB
      fs.read(reinterpret_cast&lt;char *&gt;(&amp;w), 2);//rectangle points
      fs.read(reinterpret_cast&lt;char *&gt;(&amp;h), 2);// x,y, width,height
  
      //These are the two lines where the error occurs
      unsigned nodeNum = tree.allocateNode(x*35, y*35, w*35, h*35);
      tree.insertLeaf(nodeNum); //Here is where it crashes                                   
  
  };
  </code></pre>
  
  <p>But hold your horses, this isn't the full story...</p>
  
  <hr>
  
  <h2>The weird part</h2>
  
  <p>As I diagnosed this error I came across a few interesting things.</p>
  
  <p><strong>1.</strong> The program actually runs perfectly a small percentage of the time. I set 
         up a <code>system("pause");</code> at the end of the commands to see if it will run 
         and it did! It also immediately crashed after I hit enter to past the 
         pause block.</p>
  
  <p><strong>2.</strong> This for some reason works just fine</p>
  
  <pre><code>unsigned nodeNum = tree.allocateNode();
  tree.insertLeaf(nodeNum);
  
  unsigned nodeNum2 = tree.allocateNode();
  unsigned nodeNum3 = tree.allocateNode();
  
  tree.insertLeaf(nodeNum3);
  tree.insertLeaf(nodeNum2);
  </code></pre>
  
  <p>But then this doesn't work at all, it actually crashes the program.</p>
  
  <pre><code>unsigned nodeNum = yahh.allocateNode();
  tree.insertLeaf(nodeNum);
  
  unsigned nodeNum2 = yahh.allocateNode();
  tree.insertLeaf(nodeNum2);
  
  unsigned nodeNum3 = yahh.allocateNode();
  tree.insertLeaf(nodeNum3);
  </code></pre>
  
  <p>This works too.</p>
  
  <pre><code>unsigned nodeNum2 = tree.allocateNode();
  tree.insertLeaf(nodeNum2);
  
  unsigned nodeNum2 = tree.allocateNode();
  //tree.insertLeaf(nodeNum2);
  </code></pre>
  
  <p>But this doesn't. It seems as if calling the <code>allocateNode();</code> and 
  <code>insetLeaf();</code> commands twice in order crashes the program...</p>
  
  <pre><code>unsigned nodeNum2 = tree.allocateNode();
  tree.insertLeaf(nodeNum2);
  
  unsigned nodeNum2 = tree.allocateNode();
  tree.insertLeaf(nodeNum2);
  </code></pre>
  
  <h3>Source Code</h3>
  
  <p><strong>AABBtree.h</strong></p>
  
  <pre><code>#define AABB_NULL_NODE 0xffffffff
  
  struct AABBNode {
      unsigned nodeNum;
      //AABBNode(unsigned _newNode) : _newNode(nodeNum) { };
      ~AABBNode(){ };
  };
  //==========================================================================
  //==========================================================================
  class AABBtree {
  private:
      unsigned _rootNode = AABB_NULL_NODE;
      std::vector&lt;AABBNode&gt; _nodeTable;
  public:
      AABBtree() { };
      ~AABBtree(){ };
  
      unsigned allocateNode() {
          //Allocate node and return new Node index
          unsigned nodeNum = _nodeTable.size();
          _nodeTable.emplace_back(nodeNum);
          return nodeNum;
      };
  
      void insertLeaf(unsigned&amp; nodeNum) {
          AABBNode&amp; newNode = _nodeTable[nodeNum];
  
          //Allocate a new AABB, it will be curNode and newNode new parent.
          unsigned newParentIndex = allocateNode();
          AABBNode&amp; newParent = _nodeTable[newParentIndex];
      };
  };
  </code></pre>
  
  <p><strong>Main.cpp</strong></p>
  
  <pre><code>AABBtree tree;
  int main() {
      unsigned nodeNum = tree.allocateNode();
      tree.insertLeaf(nodeNum);
      unsigned nodeNum2 = tree.allocateNode();
      tree.insertLeaf(nodeNum2);
  };
  </code></pre>
  
  <p>I've cut most of the parts out to just the bare minimums</p>