title: ERROR: LeakSanitizer: detected memory leaks
body: <p>I keep getting the following error when I run my program:
  <a href="https://i.stack.imgur.com/o6Ryk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/o6Ryk.png" alt="error message" /></a></p>
  <p>It mentions leaks in lines 15, 64, and 110. I think the error is in the push function but I do not see where I could free the variable without causing errors in my code.</p>
  <p>Line 15: <code> struct sNode* new_node = (struct sNode*)malloc(sizeof(struct sNode));</code></p>
  <p>Line 64: <code>push(&amp;stack, exp[i]);</code></p>
  <p>Line 110: <code>int n = areBracketsBalanced(argv[1]);</code></p>
  <p>This is my code:</p>
  <pre><code>#include &lt;stdbool.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  // structure of a stack node
  struct sNode {
      char data;
      struct sNode* next;
  };
  
  void push(struct sNode** top_ref, int new_data);
  int pop(struct sNode** top_ref);
  void push(struct sNode** top_ref, int new_data){
      struct sNode* new_node = (struct sNode*)malloc(sizeof(struct sNode));
  
      if (new_node == NULL) {
          printf(&quot;Stack overflow \n&quot;);
          getchar();
          exit(0);
      }
  
      new_node-&gt;data = new_data;
      new_node-&gt;next = (*top_ref);
      (*top_ref) = new_node;
  }
  int pop(struct sNode** top_ref){
      char res;
      struct sNode* top;
      
      if (*top_ref == NULL) {
          printf(&quot;Stack overflow \n&quot;);
          getchar();
          exit(0);
      }else {
          top = *top_ref;
          res = top-&gt;data;
          *top_ref = top-&gt;next;
          free(top);
          return res;
      }
  }
  
  // Returns 1 if character1 and character2 are matching left and right Brackets
  bool isMatchingPair(char character1, char character2)
  {
      if (character1 == '(' &amp;&amp; character2 == ')')
          return 1;
      else if (character1 == '{' &amp;&amp; character2 == '}')
          return 1;
      else if (character1 == '[' &amp;&amp; character2 == ']')
          return 1;
      else
          return 0;
  }
  
  // Return 1 if expression is balanced
  int areBracketsBalanced(char exp[]){
      int i = 0;
      struct sNode* stack = NULL;
  
      while (exp[i]){
          if (exp[i] == '{' || exp[i] == '(' || exp[i] == '[')
              push(&amp;stack, exp[i]);
  
          if (exp[i] == '}' || exp[i] == ')' || exp[i] == ']') {
  
              if (stack == NULL){
                  printf(&quot;%d: %c\n&quot;,i,exp[i]);
                  return 0;
              }else if (!isMatchingPair(pop(&amp;stack), exp[i])){
                  printf(&quot;%d: %c\n&quot;,i,exp[i]);
                  return 0;
              }
          }
          i++;
      }
  
      if(stack == NULL){
          return 1;
      }else{
          printf(&quot;open: &quot;);
          
              while(stack!=NULL){
               int value = pop(&amp;stack);
              if(value == '('){
                  printf(&quot;%c&quot;,')');
              }else if(value == '['){
                  printf(&quot;%c&quot;,']');
              }else if(value == '{'){
                  printf(&quot;%c&quot;,'}');
              }
              }
              printf(&quot;\n&quot;);
              while(stack!=NULL){
              pop(&amp;stack);
              }
          return 0;
      }
      
  }
  
  int main(int argc, char* argv[]){
      if(argc&gt;1){
          int n = areBracketsBalanced(argv[1]);
          if(n==0){
              return EXIT_FAILURE;
          }else if(n==1){
              return EXIT_SUCCESS;
          }   
      }
      
      //return 1;
  }
  </code></pre>