unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Olivier Dion via General Guile related discussions <guile-user@gnu.org>
To: adriano <randomlooser@riseup.net>, guile-user <guile-user@gnu.org>
Subject: Re: foreign objects and the garbage collector
Date: Tue, 21 Sep 2021 10:25:47 -0400	[thread overview]
Message-ID: <87czp2t384.fsf@laura> (raw)
In-Reply-To: <5daca356c077a108d530b7a88d2361b4a0d63ce1.camel@riseup.net>

On Tue, 21 Sep 2021, adriano <randomlooser@riseup.net> wrote:
> Hi Olivier,
>
> thank you very much for your reply
>
> Il giorno dom, 19/09/2021 alle 14.11 -0400, Olivier Dion ha scritto:
>> On Sun, 19 Sep 2021, adriano <randomlooser@riseup.net> wrote:
>> > 
>> > 
>
>
>
>
>> > 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 ()))
>
> Oh my, thank you for this !
>
> This should be included in the manual !
>
> The example that's there currently is totally indequate, in my opinion
>
>> > 
>> 
>> Says you have `open_my_resource()` and `close_my_resource()` in C in
>> library "libfoo.so" where open_my_resource takes a C string and returns
>> an integer for the resource while close_my_resource takes the integer
>> of
>> the resource:
>> 
>> (define open-my-resource%
>>   (eval-when (eval load compile)
>>     (let ((this-lib (load-foreign-library "libfoo")))
>>       (foreign-library-function this-lib "open_my_resource"
>>                                 #:return-type int
>>                                 #:arg-types (list '*))))
>> 
>> (define open-my-resource%
>>   (eval-when (eval load compile)
>>     (let ((this-lib (load-foreign-library "libfoo")))
>>       (foreign-library-function this-lib "open_my_resource"
>>                                 #:return-type int
>>                                 #:arg-types (list int)))))
>
> Uhmm... I see 2 versions of open-my-resource%
> The only slight difference I see is in the #:arg-types
>
> The first one has 
>
> (list '*) 
>
> and the second one has 
>
> (list int)
>
>
> Maybe you you got confused while editing ?

You're right.  I copy paste the form two times and forget to edit the second
binding name.  I meant `close-my-resource%` for the second define.

>
> Thank you again

Just make sure to read what Maxime says about escape continuation.
Dynamic wind are not bullet proof.

As the manual says:

        If, any time during the execution of THUNK, the dynamic extent of
        the ‘dynamic-wind’ expression is escaped non-locally, OUT_GUARD is
        called.  If the dynamic extent of the dynamic-wind is re-entered,
        IN_GUARD is called.  Thus IN_GUARD and OUT_GUARD may be called any
        number of times.

Thus, you could also do a `(set! resource #f)` after freeing it, so that
the overall dynamic-wind is reentrant and you don't end up with double
free of resource.

-- 
Olivier Dion
Polymtl

  reply	other threads:[~2021-09-21 14:25 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-04 12:41 foreign objects and the garbage collector Tim Meehan
2021-09-04 13:35 ` Vivien Kraus via General Guile related discussions
2021-09-05  0:58   ` Tim Meehan
2021-09-19  6:14   ` adriano
2021-09-19 18:11     ` Olivier Dion via General Guile related discussions
2021-09-19 18:23       ` Maxime Devos
2021-09-21 14:26         ` adriano
2021-09-21 18:49           ` Maxime Devos
2021-09-21 14:00       ` adriano
2021-09-21 14:25         ` Olivier Dion via General Guile related discussions [this message]
2021-09-21 14:51           ` adriano

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=87czp2t384.fsf@laura \
    --to=guile-user@gnu.org \
    --cc=olivier.dion@polymtl.ca \
    --cc=randomlooser@riseup.net \
    /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).