[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: foreign objects and the garbage collector
From: |
Maxime Devos |
Subject: |
Re: foreign objects and the garbage collector |
Date: |
Sun, 19 Sep 2021 20:23:07 +0200 |
User-agent: |
Evolution 3.34.2 |
Olivier Dion via General Guile related discussions schreef op zo 19-09-2021 om
14:11 [-0400]:
> On Sun, 19 Sep 2021, adriano <randomlooser@riseup.net> wrote:
> > > If you want to avoid the problem, you should explicitely bind and
> > > call the gps-close function and not rely on the garbage collector to
> > > do it for you. You can use dynamic-wind to open and close
> > > resources as needed.
> >
> > It'd be so nice to have an example
>
> (define (with-my-resource token proc)
> (let ((resource #f))
> (dynamic-wind
> (lambda ()
> (set! resource (open-my-resource% token)))
>
> (proc resource)
>
> (lambda ()
> (when resource
> (close-my-resource% resource))))))
>
> (with-my-resource "some-internal-token" (lambda ()))
FWIW, this doesn't work well with continuations, e.g. if you use guile-fibers
for concurrency. I'm not familiar with 'libgps', so I'm not sure if it would
work for you, but another method for implementing with-my-resource could be to
use the exception handling mechanism to call 'close-my-resource' on both
normal exits and abnormal exits.
Note that this alternative method does _not_ close the resource when calling
an escape continuation (let/ec (with-my-resource .. (lambda (r) (ec)
(unreachable)))),
so you might want to use something like guardians and after-gc-hook to
eventually
free the resource.
(Here, close-my-resource is like close-my-resource% except it doesn't do
anything
if the resource is already closed.)
Greetings,
Maxime.
signature.asc
Description: This is a digitally signed message part