Subject: bug#19236: load-compiled procedure leaks memory

From: Chris Vine <[email-masked]>
Date: Sun, 30 Nov 2014 23:30:09 +0000

The load-compiled procedure leaks memory in guile-2.0.11 as
demonstrated by the attached test case.  It should be used in
conjunction with a file 'test-file.scm' which contains only a single #f
expression.  This test case should be invoked either with the "pload"
or "load" option.  If invoked with the pload option, it will invoke
primitive-load, which accumulates no additional memory while executing,
and will execute normally to the end of its iterations.  If invoked
with the load option, on my 32-bit machine it will steadily accumulate
a memory leak before running out of memory on consuming approximately
300M memory, after about 65,000 iterations.  The memory leak seems to
arise in guile's load-compiled procedure.

The question which might be asked is "Would any sane person ever want
to invoke the load (or load-compiled) procedures more than a few times
in a practical program?".  The answer to this question is "Yes", if
guile is being used as an extension framework for a C or C++ program,
and it executes guile extensions as individual tasks.

Test case:

----------------------------- snip -----------------------------

/* compile with 'gcc -O2 -Wall `pkg-config --cflags --libs guile-2.0` -o test-guile' */

#include <libguile.h>
#include <stdio.h>
#include <string.h>

int load;

void* func (void* data)
{

  switch (load)
    {
      case 0:
	scm_c_eval_string("(primitive-load \"test-file.scm\")");
	break;
      default:
	scm_c_eval_string("(load \"./test-file.scm\")");
    }

  return NULL;
}

int main (int argc, char *argv[])
{

  int count;

  if (argc != 2
      || (strcmp (argv[1], "pload") &&
	  strcmp (argv[1], "load")))
    {
      puts ("Usage: test-guile load | pload");
      exit (1);
    }

  if (!strcmp (argv[1], "load"))
    {
      puts("Using load");
      load = 1;
    }
  else
    puts("Using primitive-load");

  for (count = 0; count < 256000; ++count)
    {
      scm_with_guile(func, NULL);

      if (!(count % 100)) {
	printf("%d ", count);
	fflush(stdout);
      }
    }

  puts("");
  return 0;
}