[Chilli] [PATCH] Fix: Memory leak of opt_run()

David Bird david at coova.com
Fri Mar 5 09:18:03 UTC 2010

No, I haven't tried it in chilli, because it doesn't make much sense.
The following will demonstrate my point. 

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

static void *m = 0;

int main() {
  pid_t pid = fork();
  if (pid) {
    /* parent */
    printf("%p\n", m);
  }  else {
    /* child */
    m = malloc(1);
    printf("%p\n", m);

$ gcc test.c 
$ ./a.out 

>From what you are saying, I think you expect the 'm' to be the same in
the parent as in the child. Because opt_run() does a fork and allocated
the newargs only in the child (which exits). You are then trying to free
newargs from the parent, which isn't possible. 

On Fri, 2010-03-05 at 17:57 +0900, Masashi Honma wrote:
> (2010/03/05 17:08), David Bird wrote:
> > Hello,
> >
> > I still don't see the problem.
> Have you tested as I wrote ?
> > In opt_run(), one of the first things it
> > does is a fork() and returns the parent process out of the function. The
> > child continues, and allocated the memory in question, before going into
> > execv(), which replaces the process. I believe your free(newargs_bk) in
> > options_destroy() is trying to free uninitialized memory (newargs_bk
> > isn't initialized in the parent, only the child).
> No. I printed out the address of newargs just after calloc(). And I
> printed out the address of newargs_bk just before free(newargs_bk) in
> options_destroy(). They have identical address.
> > So, the question is,
> > do you think memory is being leaked in the parent or child process? I
> > would only be concerned about leaking memory in the parent (chilli)
> > process as the child process is short-lived.
> Do you think the newargs is freed when the child process finished ?
> If so, why free() in options_destroy() doesn't cause double free
> warning in my previous test code ?
> Regards,
> Masashi Honma.

More information about the Chilli mailing list