[Gc] gc callbacks

Andy Wingo wingo at pobox.com
Fri Apr 15 03:29:09 PDT 2011


Hello list,

I would like to get a callback when GC runs.  I was doing this with
finalizers:

    #include <gc.h>
    #include <stdio.h>

    static int collected = 0;

    static void
    scm_c_register_gc_callback (void *key, void (*func) (void *, void *),
                                void *data)
    {
      if (!key)
        key = GC_MALLOC_ATOMIC (sizeof (void*));
      
      GC_REGISTER_FINALIZER_NO_ORDER (key, func, data, NULL, NULL);

      key = NULL;
    }

    static void
    system_gc_callback (void *key, void *data)
    {
      scm_c_register_gc_callback (key, system_gc_callback, data);
      fprintf (stderr, "gc running\n");
      collected++;
    }


    int main (int argc, char *argv[])
    {
      GC_INIT ();
      scm_c_register_gc_callback (NULL, system_gc_callback, NULL);
      GC_gcollect ();
      return collected ? 0 : 1;
    }

It seems to work fine for me in some cases, but not in others.  I can't
reproduce it with this test program, but in some cases Guile, doing a
similar trick, never runs the finalizer.

So it seems that something is aliasing that key value.  How would I go
about figuring out what that is?

Thanks,

Andy
-- 
http://wingolog.org/


More information about the Gc mailing list