title: Is it a leak if memory isn't freed in a child process before exit?
body: <p>There are similar question all over and one which is closes is from <a href="https://softwareengineering.stackexchange.com/questions/140483/is-it-a-waste-of-time-to-free-resources-before-i-exit-a-process/140523">this</a> stack exchange site. But even tho I learned a lot reading them none of them a exactly answer my question.</p>
  <p>Here is the question, say I have this program (it is a very simplified version)</p>
  <pre><code>inline void execute(char *cmd)
  {
      int exitstat = 0;
      //lots of other thins happed
      exitstat = execve(cmd, cmdarg, environ);
      free(cmd), free(other passed adress);  ## DO I NEED THIS, since I am already about to exit
      _exit (exitstat);
  }
  
  int main(void)
  {
      int childstat;
      char *str = malloc(6); //I also have many more allocated space in heap in the parent process
      strcpy(str, &quot;Hello&quot;);
  
      pid_t childid = fork() //creat a child process, which will also get a copy of all the heap memories even tho it is CoW.
      if (childid &lt; 0)
          exit(-1);
      if (child == 0)
      {
          execute(str);
      }
      else
      {
          wait(&amp;childstat);
          free(str);
      }
  
  
  //do somethign else with str with other functions and the rest of the program
  }
  </code></pre>
  <p>In this program, the <code>parent</code> <code>process</code> does its thing for a while, allocates a lot of process in the <code>heap</code>, <code>free</code> some, keep other for later and at some point it wants to execute some command, but it doesn't want to terminate, so it creates a <code>child</code> to do its biting.</p>
  <p>The child then calls another function which will do some task and in the end use <code>execve</code> to <code>execute</code> the command passed to it. If this was successful there would be no problem, since the executed program will handle all the allocated spaces, but if it failed the child exits with a status code. The  parent waits for the child to answer, and when it does it moves on to its next routine. The problem here is that when the <code>child</code> fails to execute all the heap data remains allocated, but does that matter? since,</p>
  <ol>
  <li>it is about to be exited int he next line,</li>
  <li>Even though I am getting started with this, I have learned that a new <code>process</code> is created during <code>fork</code>, so the memory leak shouldn't affect the <code>parent</code> since the <code>child</code> is about to die.</li>
  <li>If my assumption is correct and this leaks doesn't actually matter, why does <code>valgrind</code> lament about them?</li>
  <li>Would there be a better way to <code>free</code> all the memories in the child heap with out actually passing all the memores (<code>str,</code> and the others in this example) to the execute function and laboriously call <code>free</code> each time? does <code>kill()</code> have a mechanism for this?</li>
  </ol>
  <p><strong>Edit</strong></p>
  <p>Here is working code</p>
  <pre><code>#include &lt;unistd.h&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  inline void execute(char *cmd)
  {
      extern char **environ;
      int exitstat = 0;
      char *cmdarg[2];
      cmdarg[0] = cmd;
      cmdarg[1] = NULL;
      exitstat = execve(cmd, cmdarg, environ);
      free(cmd);
      _exit (exitstat);
  }
  
  int main(void)
  {
      int childstat;
      char *str = malloc(6);
      pid_t  childid;
  
      strcpy(str, &quot;Hello&quot;);
      childid = fork();
      if (childid &lt; 0)
          exit(-1);
      if (childid == 0)
      {
          execute(str);
      }
      else
      {
          wait(&amp;childstat);
          free(str);
      }
  
      return (0);
  }
  </code></pre>
  <p>When there is a free inside the child process here is valgrinds result.
  <a href="https://i.stack.imgur.com/20Y7c.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/20Y7c.png" alt="enter image description here" /></a></p>
  <p>When run with the <code>free</code> inside the child process is commented.(<code>#free(cmd)</code> in the execute function).</p>
  <p><a href="https://i.stack.imgur.com/tU2DK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tU2DK.png" alt="enter image description here" /></a></p>
  <p>As you might see there is no overall error (<a href="https://i.stack.imgur.com/c3gL8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/c3gL8.png" alt="enter image description here" /></a>) since this programm is short lived. But in my real program which has an infinite loop every problem in the child (<a href="https://i.stack.imgur.com/HjrZD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HjrZD.png" alt="enter image description here" /></a>) matters. So I was asking if I be worried by this leaked memories in this child process just before exiting and hunt them down or just leave them as they are in another virtual space and they are cleaned as their processes exit (but in that case why is valgrind still complaining about them if their corresponding process clean them during exit)</p>