[plt-scheme] Memory management and ?custodians

From: Norman Gray (norman at astro.gla.ac.uk)
Date: Wed Mar 3 15:40:09 EST 2010

Greetings.

Can anyone give me some advice about the use of custodians in an extension?  I think my question boils down to: how do I (effectively) enforce an ordering in the reclamation of (the Scheme_Object* wrappers of) two inter-related C objects.

A bit of background: I'm wrapping selected parts of the (C) librdf.org library.  The librdf_storage object returns (pointers to) objects called librdf_iterator (the non-obvious semantics of these things don't matter for this question) which can be examined repeatedly to produce information about the librdf_storage object.  The librdf_storage object retains some references to the librdf_iterator objects it has produced.

What this means (if my understanding of my problem is correct) is this: if the librdf_storage object is reclaimed before one of its librdf_iterator objects, the library complains.  It doesn't crash or anything, but it's clearly unhappy.  Which makes me unhappy.

[ Parenthetically: I'm slightly surprised that the iterator isn't being freed promptly, as it's inside the equivalent of:
    (let ((storage (get-storage ...))
      ; do stuff
      (let ((iterator (get-iterator storage)))
        #t)
      (collect-garbage)
      ; do more stuff with storage
      )
Is is possible that the pointer link from librdf_storage to its librdf_iterator is preventing the iterator being reclaimed when I would naively expect, even though this isn't a scheme object pointer?  No, I thought not.... ]

My impression is that I should be looking at custodians to help manage this, so that I would create a new custodian associated with each librdf_storage object, and have that custodian manage the librdf_iterator objects that storage object produces.  Is that correct?  This is the general impression I get from [1], somewhat backed up by [2], but:

  * I can't find any text that really spells it out for me; and
  * a custodian sounds like a rather heavyweight object, for rather special occasions (though this might be just those four rather grand syllables in a row giving me this impression), enough that I can't help feeling I'm barking up the wrong tree here, in some way.

That is -- a terse example would be extremely helpful in this section of the documentation, discussing what sort of situation warrants or requires the creation of a new custodian, rather than simply having the current-custodian manage everything.

I'm using 4.2.4, and CGC, on OS X.  Would my making the switch to 3m magically solve my problems? (PLT-related problems, that is).

Thanks for any pointers.

Best wishes,

Norman


[1] http://docs.plt-scheme.org/inside/Custodians.html
[2] http://docs.plt-scheme.org/reference/eval-model.html?q=custodian#(part._custodian-model)

-- 
Norman Gray  :  http://nxg.me.uk





Posted on the users mailing list.