[racket] Stopping scheme object from being GCed

From: Veer (diggerrrrr at gmail.com)
Date: Sat Jun 5 12:55:12 EDT 2010

How do i stop the scheme values being GCed

Here is roughly what happens in two files given below:
1. binds two functions connect and emit
2. when connect is called with function as its argument , it is stored
in global variable
3. when emit is called , it applies the function stored in the
callback variable.
4. during next emit , the apply function does not get the correct
scheme object/function.

I am assuming that function is garbage collected , what is the correct
way to do above?

Thanks


SCHEME FILE :
#lang scheme
(require "scheme_test.ss")
(connect (lambda () (printf "ok\n")))
(emit)

and in repl :
> (emit)
> (build-list 1000 (lambda (i) i))
> (emit)
> procedure application: expected procedure, given: #<bad-value> (no arguments)




C FILE :

#define MZ_PRECISE_GC
#include "escheme.h"

void *callback ;

void scheme_generic(void *data)
{
  Scheme_Object* proc = (Scheme_Object *) data;
  scheme_apply(proc , 0 , NULL);
}

Scheme_Object* scheme_emit(int argc , Scheme_Object* argv[])
{
  scheme_generic(callback);
  return scheme_void;
}

Scheme_Object* scheme_connect(int argc , Scheme_Object* argv[])
{
  callback = argv[0];
  return scheme_void;
}

Scheme_Object* scheme_initialize (Scheme_Env* env)
{
  Scheme_Env* mod_env = NULL;
  callback = NULL;
  MZ_GC_DECL_REG(2);
  MZ_GC_VAR_IN_REG(0,env);
  MZ_GC_VAR_IN_REG(1,mod_env);
  MZ_GC_REG();

  mod_env=scheme_primitive_module(scheme_intern_symbol("scheme_test"),env);
  scheme_add_global("connect"
,scheme_make_prim_w_arity(scheme_connect,"connect",1,1),mod_env);
  scheme_add_global("emit"
,scheme_make_prim_w_arity(scheme_emit,"emit",0,0),mod_env);
  scheme_finish_primitive_module(mod_env);
  MZ_GC_UNREG();
					
  return scheme_void;

}
Scheme_Object* scheme_reload (Scheme_Env* env)
{
  return scheme_initialize(env);
}

Scheme_Object* scheme_module_name (void)
{
  return scheme_intern_symbol("scheme_test");
}


Posted on the users mailing list.