unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: "Kjetil S. Matheussen" <k.s.matheussen@notam02.no>
To: guile-user@gnu.org
Subject: How to use guardians
Date: Thu, 13 Mar 2008 15:12:10 +0100 (CET)	[thread overview]
Message-ID: <Pine.LNX.4.64.0803131456260.2451@ttleush> (raw)


Hi,

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")))
   (gc)
   (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))
   `(begin
      (let ,loop ((guard (das-guardian)))
 	  (when guard
 	    ((car guard) (cadr guard))
 	    (,loop (das-guardian))))
      (das-guardian (list ,cleanup-func ,object))))





             reply	other threads:[~2008-03-13 14:12 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-03-13 14:12 Kjetil S. Matheussen [this message]
2008-03-13 14:19 ` How to use guardians Thien-Thi Nguyen
  -- strict thread matches above, loose matches on Subject: below --
2008-03-13 15:02 Kjetil S. Matheussen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.64.0803131456260.2451@ttleush \
    --to=k.s.matheussen@notam02.no \
    --cc=guile-user@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).