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

From: [email-masked] (Ludovic Courtès)
Date: Sun, 07 Oct 2012 22:38:33 +0200

Hi,

Daniel Hartwig <[email-masked]> skribis:

> On 7 October 2012 05:41, Ludovic Courtès <[email-masked]> wrote:
>> 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.)
>
> If I understand correctly, there is never any non-weak reference to
> the bv above and so it can be collected at any time.

There’s a weak reference from the pointer object to the bytevector.

Once that pointer object has been collected (as in the example above),
the bytevector can be collected anytime.

Thanks,
Ludo’.