> Mike Gran <spk...@yahoo.com> skribis:
>> 3. Some versions of Guile have a bug where the last smob defined
>> in your program is never freed, even if you've cleared out the
>> pointers and called scm_gc.
> What is this? I’d be happy to read more details. :-)
To be more specific, if you want to make sure the GC frees every smob,
you should call scm_gc before the program ends.
But, if you place the scm_gc call inside of a function
designated by C's atexit() function, instead of, say, at the end of
main(), it may not free all the SMOBs. I never investigated why.
I assumed it was a race.
So, it is probably not really a Guile bug, but, rather, that calling
scm_gc from within atexit() is a bad idea.
The program below gives me the following output in Guile 2.0.9
Allocating #1
Allocating #2
Allocating #3
Allocating #4
Allocating #5
Allocating #6
Allocating #7
Allocating #8
Allocating #9
Allocating #10
Freeing #5
Freeing #6
Freeing #3
Freeing #4
Freeing #2
Freeing #1
gc at exit
Freeing #9
Freeing #7
Freeing #8
-Mike
#include <libguile.h>
#include <stdio.h>
#define SIZE (1000000 * sizeof(int))
static scm_t_bits model_smob_t;
static size_t free_model(SCM smob)
{
int* ptr = (int *)SCM_SMOB_DATA(smob);
printf("Freeing #%d\n", *ptr);
fflush(stdout);
scm_gc_free (ptr, SIZE, "int array");
ptr = NULL;
return 0;
}
void func(void)
{
printf("gc at exit\n");
fflush(stdout);
scm_gc();
}
int main (int argc, char ** argv)
{
SCM s;
atexit(func);
scm_init_guile();
model_smob_t = scm_make_smob_type("model", sizeof(int *));
scm_set_smob_free(model_smob_t, free_model);
for (int i = 1; i <= 10; i ++)
{
printf("Allocating #%d\n", i);
int *ptr = (int *) scm_gc_malloc (SIZE, "int array");
*ptr = i;
SCM_NEWSMOB(s, model_smob_t, (scm_t_bits) ptr);
s = SCM_BOOL_F;
}
// printf("gc at end of main\n");
// fflush(stdout);
//scm_gc();
return 0;
}