title: Memory Leaking, I can't seem to find why
body: <p>So I am writing a program for managing a heap data structure. I make two dynamic memory allocations and (I think) I free them correctly when im wrapping things up.</p>
  
  <pre><code>#include "heapFunctions.h"
  #include "util.h"
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  //Function Prototypes
  static element* getArray(int*);
  
  int main(void){
  int result=0;
  int i,v;
  heap myHeap;
  myHeap.H = NULL;
  int arrayLength;
  element* myArray = NULL;
  char menuSelection = nextCommand(&amp;i,&amp;v);        //get selection from user
  while(!(menuSelection == 'S' || menuSelection == 's')){
      switch(menuSelection){
          case 'c':
          case 'C':
              if(myHeap.H == NULL)
                  myHeap = initialize(i);         //initialize heap and identify any errors
              else{
                  free(myHeap.H);
                  myHeap=initialize(i);
              }
              if(myHeap.H != NULL)
                  printf("Command Entered %c. Heap Initialized with capacity %d\n", menuSelection, i);
              else
                  printf("Command Entered %c. Heap space not allocated\n", menuSelection);
              break;
          case 'r':
          case 'R':
              if(myArray == NULL)
                  myArray = getArray(&amp;arrayLength);               //populate array from text file
              else{
                  free(myArray);
                  myArray = getArray(&amp;arrayLength);
              }
              result=buildHeap(&amp;myHeap, myArray, arrayLength);        //build heap with array
              if(result==1)
                  printf("Command Entered %c. Heap was built with size %d\n", menuSelection, arrayLength);
              else if (result == -1)
                  printf("Command Entered %c. Heap build was unsuccesful\n", menuSelection);
              else if (result == -2)
                  printf("Command Entered %c. Heap capacity can't accomodate array\n", menuSelection);
              break;
          case 'w':
          case 'W':
              printf("Command Entered %c. Printing Heap\n", menuSelection);
              printHeap(&amp;myHeap);                 //print contents of heap
              break;
          case 'i':
          case 'I':
              result = insert(&amp;myHeap, i);            //insert new key i into heap
              if (result == 1)
                  printf("Command Entered %c. Heap insert with key %d was succesful\n", menuSelection, i);
              else
                  printf("Command Entered %c. Heap insert was unsuccesful\n", menuSelection);
              break;
          case 'd':
          case 'D':
              result = deleteMax(&amp;myHeap);        //extract max value from heap
              if (result &gt; 0)
                  printf("Command Entered %c. Deletion of max heap value %d was succesful\n", menuSelection, result);
              break;
          case 'k':
          case 'K':
              result = increaseKey(&amp;myHeap, i, v);            //increase key at index i to v
              if(result == 1)
                  printf("Command Entered %c. Key was succesfully increased to %d at index %d\n", menuSelection, v, i);
              else if(result == -1)
                  printf("Command Entered %c. Key increase failed, %d not a valid index\n", menuSelection, i);
              else if (result == -2)
                  printf("Command Entered %c. Key increase failed, %d is not larger than current key\n", menuSelection, v);
              else if (result == -3)
                  printf("Command Entered %c. Key increase failed, Index starts at 1!", menuSelection);
      }
      menuSelection = nextCommand(&amp;i,&amp;v); 
  }
  printf("You have entered command %c and stopped the program.\n", menuSelection);
  
  //free resources
  free(myArray);
  free(myHeap.H);
  return 1;
  }
  
  //get array from text file for heap
  static element* getArray(int *Length){
      element *arrayKey;          //declare pointer for new array
      int arrayLength=0;
      char inputBuffer[10];
      FILE *fp;
      fp = fopen("HEAPinput.txt","r");            //open text file
      if (fp == NULL){                    /*check to make sure file was opened*/
          fprintf(stderr, "Cannot open input file!!\n");
          exit(1);
      }
      if(fgets(inputBuffer, sizeof(inputBuffer), fp) != NULL){        //get line of text
          sscanf(inputBuffer, "%d", &amp;arrayLength);                //parse line for number of inputs
      }
  
      if(arrayLength &lt; 1){                //error if array length is invalid
          printf("Invalid Array Length\n");
          exit(1);
      }
  
      arrayKey = (element *) malloc(sizeof(element)*arrayLength);     //dynamically allocate memory for values
      if(arrayKey == NULL){
          printf("Memory for array not allocated\n");
           exit(1);
      }   
      int count;
  
      for (count =0; count &lt; arrayLength; count++){               //populate array with input from file
          fscanf(fp, "%d", &amp;arrayKey[count].key);
      }
  
      *Length = arrayLength;  
      fclose(fp);                                             //close file
      return arrayKey;                        //return array      
  }
  
  
  //initialize new heap with size 0 and designated capacity
  heap initialize(int capacity){
      heap myHeap;
      myHeap.size = 0;
      myHeap.capacity = capacity;
      myHeap.H = (element*) malloc(sizeof(element)*capacity);         //dynamically allocate memory blocks for heap with designated capacity
      return myHeap;
  }
  
  //copy contents of heap into H element,
  int buildHeap(heap *myHeap, element * myArray, int arrayLength){
      if(arrayLength &gt; myHeap-&gt;capacity)      //error if capacity is to small
          return -2;
      if(myHeap-&gt;H == NULL)
          return -3;
      if(memcpy(myHeap-&gt;H, myArray, sizeof(element)*arrayLength) == NULL)     //error if memory not allocated properly
          return -1;
  
      myHeap-&gt;size=arrayLength;               //set size to arrayLength
  
      int count=0;
      for(count=(arrayLength/2); count &gt;= 0; count--){            //buildHeap
          heapify(myHeap, count);
      }
      return 1;
  
  }
  </code></pre>
  
  <p>Im not really sure how this works, i tried just posting the pieces of code i thought were necesarry. I only dynamically allocate memory in two spots and I thought I freed them properly before I leave main. I don't see where else I could be leaking.</p>
  
  <p>I used valgrind and got the errors</p>
  
  <pre><code> LEAK SUMMARY:
   ==4042==    definitely lost: 13,546 bytes in 70 blocks
   ==4042==    indirectly lost: 53 bytes in 5 blocks
   ==4042==      possibly lost: 29 bytes in 2 blocks
   ==4042==    still reachable: 33,958 bytes in 53 blocks
  </code></pre>
  
  <p>I also had it print the whole trace (with debugging symbols) but all the outputs were pretty much the same (repeats the following for most blocks). I tried compiling with gcc + g++ with valgrind with the -g flag and fulltrace but i still just get ??? for the output after mem location.</p>
  
  <pre><code>==5804== 3 bytes in 1 blocks are possibly lost in loss record 2 of 97
  ==5804==    at 0x4C2C73C: malloc (vg_replace_malloc.c:270)
  ==5804==    by 0x440137: ??? (in /usr/bin/g++-4.7)
  ==5804==    by 0x43CDEB: ??? (in /usr/bin/g++-4.7)
  ==5804==    by 0x414C80: ??? (in /usr/bin/g++-4.7)
  ==5804==    by 0x41592F: ??? (in /usr/bin/g++-4.7)
  ==5804==    by 0x40296E: ??? (in /usr/bin/g++-4.7)
  ==5804==    by 0x4E5576C: (below main) (libc-start.c:226)
  
  
  349 (320 direct, 29 indirect) bytes in 2 blocks are definitely lost in loss record          73 of 96
  ==4098==  at 0x4C2C92E: realloc (vg_replace_malloc.c:662)
  </code></pre>
  
  <p>Can anyone point me in the right direction, as to why I am leaking memory. </p>