[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How to use guardians

From: Kjetil S. Matheussen
Subject: How to use guardians
Date: Thu, 13 Mar 2008 15:12:10 +0100 (CET)


I'm trying to use guardians, but have a little bit of
trouble understanding how they work.

I have made a function "add-guardian-object" which
takes an object "object" and a function "cleanup-func".
"cleanup-func" is called before the object is freed.

Here is my implementation of "add-guardian-object":

  (define das-guardian (make-guardian))

  (define (add-guardian-object object cleanup-func)
    (let loop ((guard (das-guardian)))
      (when guard
        ((car guard) (cadr guard))
        (loop (das-guardian))))
    (das-guardian (list cleanup-func object)))

First try
I run a test evaluating the following expressions:

  (define alist (list 'unique-name 2 3 4))
  (add-guardian-object alist (lambda (object)
                               (c-display object "was garbage collected")))
  (begin das-guardian)

But "(begin das-guardian)" shows:

  #<guardian 8a8a8c0 (reachable: 0 unreachable: 1)>

Which means that the reference to "alist" is now unreachable.
But why is that so? "alist" is a global reference
and should therefore definitely be reachable.

Is this a bug in guile or have I misunderstood something?

Second try
Does "unreachable" mean that it is only unreachable from
within the "add-guardian-object" function? In case,
why doesn't it work just changing "add-guardian-object"
into a mcaro like this?

(define-macro (add-guardian-object object cleanup-func)
  (define loop (gensym))
     (let ,loop ((guard (das-guardian)))
          (when guard
            ((car guard) (cadr guard))
            (,loop (das-guardian))))
     (das-guardian (list ,cleanup-func ,object))))

reply via email to

[Prev in Thread] Current Thread [Next in Thread]