* How to use guardians
@ 2008-03-13 14:12 Kjetil S. Matheussen
2008-03-13 14:19 ` Thien-Thi Nguyen
0 siblings, 1 reply; 3+ messages in thread
From: Kjetil S. Matheussen @ 2008-03-13 14:12 UTC (permalink / raw)
To: guile-user
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))))
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: How to use guardians
2008-03-13 14:12 How to use guardians Kjetil S. Matheussen
@ 2008-03-13 14:19 ` Thien-Thi Nguyen
0 siblings, 0 replies; 3+ messages in thread
From: Thien-Thi Nguyen @ 2008-03-13 14:19 UTC (permalink / raw)
To: guile-user
() "Kjetil S. Matheussen" <k.s.matheussen@notam02.no>
() Thu, 13 Mar 2008 15:12:10 +0100 (CET)
(das-guardian (list cleanup-func object)))
Here, you add the compound object (constructed using `list'),
not just `object'. Is that what you want do, really?
thi
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: How to use guardians
@ 2008-03-13 15:02 Kjetil S. Matheussen
0 siblings, 0 replies; 3+ messages in thread
From: Kjetil S. Matheussen @ 2008-03-13 15:02 UTC (permalink / raw)
To: guile-user
Thien-Thi Nguyen:
> (das-guardian (list cleanup-func object)))
>
> Here, you add the compound object (constructed using `list'),
> not just `object'. Is that what you want do, really?
Ah, of course. Thank you very much. Also for the mysteriously
quick reply. :-)
I have now changed "add-guardian-object" into:
(define das-guardians '())
(define (add-guardian-object object cleanup-func)
(set! das-guardians
(remove (lambda (das-guardian)
(let ((guard ((cadr das-guardian))))
(and guard
(begin
((car das-guardian) guard)
#t))))
das-guardians))
(push! (list cleanup-func
(let ((guardian (make-guardian)))
(guardian object)
guardian))
das-guardians))
And this seems to work. Great!
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2008-03-13 15:02 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-03-13 14:12 How to use guardians Kjetil S. Matheussen
2008-03-13 14:19 ` Thien-Thi Nguyen
-- strict thread matches above, loose matches on Subject: below --
2008-03-13 15:02 Kjetil S. Matheussen
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).