title: c++ receives segfault through GDB but not xCode -- need help debugging
body: <p>I'm working on a project and have recently started running into a particularly nasty segfault. Here is some background:</p>
  
  <p>1 -- I have a queue of "orders" on which I have tested the following:
  a -- filling up (0 orders, 1 order, and many orders), removed until isEmpty() returns true, and filled other queues with these results (this replicates the programs behavior)</p>
  
  <p>2 -- found this from gdb:</p>
  
  <pre><code>(gdb) run
  Starting program: /Users/Nicholas M. Iodice/Dropbox/tufts/noz2/noz2/a.out 
  Reading symbols for shared libraries ++......................... done
  order added 1
  
  Program received signal EXC_BAD_ACCESS, Could not access memory.
  Reason: 13 at address: 0x0000000000000000
  0x0000000100003a66 in Queue::remove (this=0x7fff5fbffaa0) at Queue.cpp:85
  85      ElementType retVal = front-&gt;order;
  (gdb) where
  #0  0x0000000100003a66 in Queue::remove (this=0x7fff5fbffaa0) at Queue.cpp:85
  #1  0x0000000100002c08 in Packer::packItem (this=0x7fff5fbffaa0, time=1) at Packer.cpp:88
  #2  0x0000000100002e3f in Packer::update (this=0x7fff5fbffaa0, time=1) at Packer.cpp:131
  #3  0x0000000100002e9c in PackerManager::update (this=0x7fff5fbffaa0, i=1) at Packer.cpp:295
  #4  0x0000000100001ffa in Manager::run (this=0x7fff5fbff830, careAboutSupreme=false) at Manager.cpp:126
  #5  0x000000010000511a in main (argc=0, argv=0x7fff5fbffb48) at main.cpp:173
  (gdb) 
  </code></pre>
  
  <p>the "order added 1" is some logging I've added in, and it occurs when my "Fetcher" class sends an order to the "PackerManager" class, which determines which "Packer" object to add an order to. I modified my code so that all orders are routed through ONE "Packer" object, for debugging purposes.</p>
  
  <p>3 -- This means that when the "Packer" object calls packItem (which will call remove() from my Queue, there is just one order in that queue. I have tested this outside of these classes, and it works fine. </p>
  
  <ol>
  <li>Here is some relevant code: </li>
  </ol>
  
  <p>from "Queue"</p>
  
  <pre><code>Queue::Queue()
  {
      front = NULL;
      back = NULL;
  
      //head = NULL;
      //tail = NULL;
      count = 0;
  }
  
  //instert to back of queue
  void Queue::insert(ElementType order)
  {
      if(isEmpty()){
          back = new Node;
          back-&gt;order = order;
          back-&gt;next = NULL;
          front = back;
      }else{
          Node* tmp = new Node;
          tmp-&gt;order = order;
          back-&gt;next = tmp;
          back = back-&gt;next;
          back-&gt;next = NULL;
      }
      count++;
  }
  
  //checks if head &amp; tail = NULL
  bool Queue::isEmpty()
  {
      if(front == NULL &amp;&amp; back == NULL){
          return true;
      }
      return false;
  }
  </code></pre>
  
  <p>throws segfault on this line: "ElementType retVal = front->order;"</p>
  
  <pre><code>ElementType Queue::remove()
  {
      if(isEmpty()){
          cout &lt;&lt; "\n\tYou cannot remove from an empty Queue... Exiting.\n\n";
          exit(1);
      }
  
      ElementType retVal = front-&gt;order;
      //delete front;
      if(front == back){
          front = NULL;
          back = NULL;
      }else{
          front = front-&gt;next;
      }
  
      count --;
      return retVal;
  }
  </code></pre>
  
  <p>this code calls remove from the queue. The one it is calling is "regularOrders"</p>
  
  <pre><code>//retreives next order to begin packing
  void Packer::packItem(int time)
  {
      if( careAboutSupreme &amp;&amp; !supremeOrders.isEmpty() ){
          orderBeingPacked = supremeOrders.remove();
          orderBeingPackedStartTime = time;
  
      }else if( careAboutSupreme &amp;&amp; isDelayOrderWaiting ){
          orderBeingPacked = orderBeingDelayed;
          orderBeingPackedStartTime = orderBeingDelayedStart + delayDuration;
          delayDuration = 0;
          isDelayOrderWaiting = false;
  
      }else if( !regularOrders.isEmpty() ){
          orderBeingPacked = regularOrders.remove();
          orderBeingPackedStartTime = time;
      }
  }
  </code></pre>
  
  <p>Also, the queue passes this test with values i = 1 and i > 1:</p>
  
  <pre><code>#if Q
      Queue q1;
      Queue q2;
      for(int i=0 ; i&lt;5 ; i++){
          Order o;
          q1.insert(o);
      }
  
      while(!q1.isEmpty()){
          q2.insert(q1.remove());
      }
  
      sortQueueByOrderNumber(q2);
  
      while(!q2.isEmpty()){
          q2.remove();
      }
      cout &lt;&lt; "Expecting to exit now" &lt;&lt; endl;
      q2.remove();
  #endif
  
  void sortQueueByOrderNumber(Queue q)
  {
      if(q.getCount() == 0){
          return;
      }
      int cnt = q.getCount();
      Order* orderArray = new Order[cnt];
      Order tmpOrder;
  
      //put orders into array -- after this they are in order, but in an array
      while(q.getCount() != 0){
          tmpOrder = q.remove();
          orderArray[tmpOrder.orderNum-1] = tmpOrder;
      }
  
      //now we stuff em back into the queue, in order!
      for(int i=0 ; i&lt;cnt ; i++){
          tmpOrder = orderArray[i];
          q.insert(tmpOrder);
      }
      delete[] orderArray;
  }
  </code></pre>
  
  <p>Please let me know what else I can provide.</p>
  
  <p>EDIT:
  It looks like a null pointer isnt the issue here: I tried adding this:</p>
  
  <pre><code>ElementType retVal;
  if(front == NULL){
      cout &lt;&lt;"oops!";
  }else{
      retVal = front-&gt;order;
  }
  </code></pre>
  
  <p>The code still falls into assigning retVal = front->order. So front != NULL, however, it thinks that front->order is a bad memory location. </p>
  
  <p><strong>EDIT 2:</strong>
  ok, this is weird. when compiling with clang++ it actually works (sorry if i stated otherwise earlier), just not g++. WHAT?</p>
  
  <p><strong>EDIT 3</strong>
  Ok, so I have scp'd these files to another machine and tried to run it. Now I am getting segmentation faults with both g++ and clang++, however, in a completley different area unrelated to the queue at all. </p>
  
  <p>Is there something else I should be looking at that could cause a more widespread memory issue? </p>
  
  <p>The segfault now occurs when trying to update one of the packer objects. The code is below, and it fails updating the time (this->time = time) -- I dont think this should happen, which is why I'm thinking there may be a larger issue at hand. Also, i checked with GDB, but this->time is not NULL.</p>
  
  <pre><code>void Packer::update(int time)
  {
      this-&gt;time = time;
  
      //if orderNum = 0 we havent gotten a real order yet. This is the default from the constructor for Order
      if(orderBeingPacked.orderNum == 0){
          packItem(time);
      }
  </code></pre>