Subject: bug#12095: Protecting pointer on bytevector with guardian does not protect memory

From: [email-masked] (Ludovic Courtès)
Date: Sat, 06 Oct 2012 23:41:03 +0200

Hi,

Daniel Hartwig <[email-masked]> skribis:

> (define x (bytevector->pointer (make-bytevector len 1)))
> (define a (pointer-address x))
> (display x)(newline)
> (my-guardian x)
> ;(my-guardian (pointer->bytevector x len))
> (set! x #f)
>
> (define (dump-struct)
>   (write (pointer->bytevector (make-pointer a) len))(newline))

This is expected to fail: ‘bytevector->pointer’ creates a weak reference
from the returned pointer object to the given bytevector.  So when the
pointer object is reclaimed, the bytevector can be reclaimed too, hence
the problem you’re observing.  (And no, guardians don’t protect objects
from garbage collection.)

To put it differently, memory management is left to the user.  The weak
reference I mention is a convenience for simple cases, but for “real
world” situations, one has to take all steps necessary to ensure that
the lifetime of C objects and that of their Scheme counterparts is in sync.

Hope this helps,
Ludo’.