From: ludo@gnu.org (Ludovic Courtès)
To: guile-devel@gnu.org
Subject: ‘set-cdr!’ and weak-cdr pairs
Date: Sun, 13 Mar 2011 16:25:07 +0100 [thread overview]
Message-ID: <87fwqrqbzg.fsf@gnu.org> (raw)
Hello,
A bit of explanation for commit
ca33b501a93f8de389c1e3e1bc987f63b6912029...
Try this:
--8<---------------cut here---------------start------------->8---
(use-modules (srfi srfi-1)
(srfi srfi-9))
(define-record-type <foo>
(make-foo x)
foo?
(x foo-x))
(define register!
(let ((t (make-weak-value-hash-table 10)))
(lambda (x)
(let ((k+v (hash-create-handle! t x #f)))
(or (cdr k+v)
(let ((o (make-foo x)))
(set-cdr! k+v o)
o))))))
(every (lambda (x)
(make-foo #f)
(let ((o (register! x)))
(or (foo? o)
(pk 'bad! o))))
(circular-list 1 2 3 4 5 6 7 8 9))
--8<---------------cut here---------------end--------------->8---
Eventually ‘(foo? o)’ fails and Guile segfaults while trying to display O.
Changing ‘register!’ to the following works:
--8<---------------cut here---------------start------------->8---
(define register!
(let ((t (make-weak-value-hash-table 10)))
(lambda (x)
(or (hash-ref t x)
(let ((o (make-foo x)))
(hash-set! t x o)
o)))))
--8<---------------cut here---------------end--------------->8---
The problem is that ‘hash-create-handle!’ above created a weak-cdr
pair—i.e., a pair whose cdr is /not/ scanned for pointers—but ‘set-cdr!’
did not register a disappearing link from O to K+V. Consequently, O
eventually gets collected, but K+V remains; the storage of O then gets
reused, and the cdr of K+V ends up containing either an unrelated or >an
invalid Scheme object.
This problem is explicitly addressed in ‘scm_hash_fn_set_x’. AFAICS
‘set-cdr!’ has no way of knowing whether its passed a normal pair or a
weak-cdr one, so it cannot be changed to handle weak-cdr pairs
gracefully.
And of course, we have the same problem with weak-car pairs and
‘set-car!’, but ‘set-car!’ is unlikely to be used on weak-car pairs.
Thoughts?
Ludo’.
next reply other threads:[~2011-03-13 15:25 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-03-13 15:25 Ludovic Courtès [this message]
2011-03-27 11:22 ` ‘set-cdr!’ and weak-cdr pairs Andy Wingo
2011-03-27 13:29 ` Ludovic Courtès
2011-05-01 21:14 ` Andy Wingo
2011-05-05 18:20 ` Ludovic Courtès
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=87fwqrqbzg.fsf@gnu.org \
--to=ludo@gnu.org \
--cc=guile-devel@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).