From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Kjetil S. Matheussen" Newsgroups: gmane.lisp.guile.user Subject: How to use guardians Date: Thu, 13 Mar 2008 15:12:10 +0100 (CET) Message-ID: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7BIT X-Trace: ger.gmane.org 1205417606 27595 80.91.229.12 (13 Mar 2008 14:13:26 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 13 Mar 2008 14:13:26 +0000 (UTC) To: guile-user@gnu.org Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Thu Mar 13 15:13:45 2008 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1JZoBZ-0004UX-UV for guile-user@m.gmane.org; Thu, 13 Mar 2008 15:13:34 +0100 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1JZoAz-0007mM-Mk for guile-user@m.gmane.org; Thu, 13 Mar 2008 10:12:57 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1JZoAv-0007k9-4C for guile-user@gnu.org; Thu, 13 Mar 2008 10:12:53 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1JZoAt-0007gr-LE for guile-user@gnu.org; Thu, 13 Mar 2008 10:12:52 -0400 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1JZoAt-0007fy-6m for guile-user@gnu.org; Thu, 13 Mar 2008 10:12:51 -0400 Original-Received: from smtp.getmail.no ([84.208.20.33]) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1JZoAs-0001Nq-Jv for guile-user@gnu.org; Thu, 13 Mar 2008 10:12:50 -0400 Original-Received: from pmxchannel-daemon.no-osl-m323-srv-009-z2.isp.get.no by no-osl-m323-srv-009-z2.isp.get.no (Sun Java System Messaging Server 6.2-7.05 (built Sep 5 2006)) id <0JXO0000XA523500@no-osl-m323-srv-009-z2.isp.get.no> for guile-user@gnu.org; Thu, 13 Mar 2008 15:12:38 +0100 (CET) Original-Received: from smtp.getmail.no ([10.5.16.1]) by no-osl-m323-srv-009-z2.isp.get.no (Sun Java System Messaging Server 6.2-7.05 (built Sep 5 2006)) with ESMTP id <0JXO00BSZA4DMP70@no-osl-m323-srv-009-z2.isp.get.no> for guile-user@gnu.org; Thu, 13 Mar 2008 15:12:13 +0100 (CET) Original-Received: from cm-84.215.136.96.getinternet.no ([84.215.136.96]) by no-osl-m323-srv-009-z1.isp.get.no (Sun Java System Messaging Server 6.2-7.05 (built Sep 5 2006)) with ESMTP id <0JXO00IJTA4DW420@no-osl-m323-srv-009-z1.isp.get.no> for guile-user@gnu.org; Thu, 13 Mar 2008 15:12:13 +0100 (CET) X-X-Sender: kjetil@ttleush X-detected-kernel: by monty-python.gnu.org: Solaris 10 (beta) X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: guile-user-bounces+guile-user=m.gmane.org@gnu.org Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.user:6454 Archived-At: 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: # 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))))