title: C: Moving free statements into separate function causes memory leak
body: <p>I have the following structure and initialisation of it in a C file:</p>
  
  <pre><code>struct Lattice {
      int length;
      int** latt;
  };
  
  struct Lattice* Create(int size){
      struct Lattice latt;
      int i,j,k,l;
  
      latt.length = size;
      latt.latt = malloc(sizeof(int*) * latt.length);
  
      for(i=0; i&lt;latt.length; i++){
          latt.latt[i] = malloc(sizeof(int) * latt.length);
      }
  
          for(j=0; j&lt;latt.length; j++){
              for(k=0; k&lt;latt.length; k++){
                  latt.latt[j][k] = 0;
              }
          }
  
      struct Lattice* lattp = &amp;latt;
      return lattp;
  </code></pre>
  
  <p>And I also have the memory freeing function:</p>
  
  <pre><code>void Destroy(struct Lattice* lattp){
      int l;
      for (l=0; l&lt;(lattp-&gt;length); l++){
          free(lattp-&gt;latt[l]);
      }
      free(lattp-&gt;latt);
  }
  </code></pre>
  
  <p>But whenever I run the code:</p>
  
  <pre><code>int main(){
      struct Lattice* lattp = Create(5);
      Destroy(lattp);
  
      return 0;
  }
  </code></pre>
  
  <p>I get a memory leak (according to Valgrind it is 40 bytes definitely lost and a further 80 indirectly lost, if this makes any difference). </p>
  
  <p>But if I have identical code except that I write the free statements, which are identical to the ones in Destroy, into Create (and modify the return statements appropriately), I don't get a memory leak. Why does moving the free statements into a separate function cause a memory leak?</p>