[plt-scheme] scheme_close_managed

From: Chongkai Zhu (czhu at cs.utah.edu)
Date: Wed Jun 27 16:27:12 EDT 2007

I have this program that "an extension allocates resources that must be 
explicitly freed (in the same way that a port must be explicitly closed)":

static void f_close(Scheme_Object *o, void *data)
    MyClose( SCHEME_CPTR_VAL(o));

static Scheme_Object *open_p(int argc, Scheme_Object **argv)
    Scheme_Object *ret;
    void *p;

    scheme_custodian_check_available(NULL, "current custodian", "");
    ret = scheme_make_cptr( p, scheme_intern_symbol( "p"));

    scheme_add_managed( NULL, ret, &f_close, NULL, 1);

    return ret;

static Scheme_Object *close_p(int argc, Scheme_Object **argv)
    scheme_remove_managed( NULL, argv[0]);
    MyClose( SCHEME_CPTR_VAL(argv[0]));
    return scheme_void;

MyOpen and MyClose actually do the open/close. open_p / close_p are the 
exported interface (with custodian). But it seems that I'm not using 
scheme_remove_managed correctly:

If I allocate the resource by calling open_p, it will be freed by the 
custodian; If I call close_p explicitly and later close the custodian, a 
error happens, which looks like the custodian is trying to close it 
again. Is this because I discard the Custodian_Reference? Thanks in advance.


Posted on the users mailing list.