<div dir="ltr"><div>That explains a lot, thanks. Just found the relevant doc part:<br><a href="http://docs.racket-lang.org/foreign/intro.html?q=_string#%28part._.Pointers_and_.Manual_.Allocation%29">http://docs.racket-lang.org/foreign/intro.html?q=_string#%28part._.Pointers_and_.Manual_.Allocation%29</a><br>
</div><div>(and next subsection)<br></div><div>Maybe just adding a link to this paragraph under the doc for `_string' could be useful then.<br></div><div><br></div>Laurent<br></div><div class="gmail_extra"><br><br><div class="gmail_quote">
On Sat, Jun 15, 2013 at 5:15 PM, Matthew Flatt <span dir="ltr"><<a href="mailto:mflatt@cs.utah.edu" target="_blank">mflatt@cs.utah.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
With<br>
<br>
(fun _string -> _void)<br>
<br>
keep in mind that `_string' means that a Racket-level Unicode string is<br>
converted (by UTF-8 encoding) to a newly allocated array of bytes.<br>
Nothing that you do to with Racket string affects the memory management<br>
of the allocated bytes.<br>
<br>
The byte string is allocated as GCable, which means that the bytes can<br>
move or be collected. That works fine as long as the foreign function<br>
is effectively atomic (i.e., no GCs happen between the call and return<br>
of the foreign function).<br>
<br>
If the foreign function retains the pointer you give it, then you'll<br>
need to take much more control. Either use `(malloc 'raw ...)' and<br>
`free', or use `(malloc 'atomic-interior ...)' and ensure that the<br>
result remains reachable for as long as its referenced by the foreign<br>
function. Also, use the type<br>
<br>
(fun _pointer -> _void)<br>
<br>
for the foreign function, so that the pointer you allocate is passed<br>
through.<br>
<div class="HOEnZb"><div class="h5"><br>
<br>
At Sat, 15 Jun 2013 08:26:39 -0400, Hendrik Boom wrote:<br>
> On Sat, Jun 15, 2013 at 02:15:04PM +0200, Laurent wrote:<br>
> > On Sat, Jun 15, 2013 at 1:55 PM, Roman Klochkov <<a href="mailto:kalimehtar@mail.ru">kalimehtar@mail.ru</a>> wrote:<br>
> ><br>
> > > What about<br>
> > ><br>
> > > (define-lib foo (fun _string -> _void))<br>
> > ><br>
> > > (foo "Test")<br>
> > ><br>
> > > ?<br>
> > ><br>
> > > Will be the pointer from string "Test" freed?<br>
> > ><br>
> ><br>
> > I *think* Racket will garbage collect the "Test" value when it is not used<br>
> > anymore on the Racket side, but it won't know what happens on the ffi C<br>
> > side (hence the "ffi/unsafe").<br>
> > So I think the lib may point to a freed value if you're not cautious.<br>
> ><br>
> ><br>
> > > And how to make it retain, if I need to save it on the foreign side?<br>
> > ><br>
> ><br>
> > You can probably at least simply store it in a global variable. There may<br>
> > be a better way to do it though.<br>
> > For example (not tested) :<br>
> > #lang racket<br>
> > (define dontfreethem '())<br>
> > (define (dontfreeme x)<br>
> > (set! dontfreethem (cons x dontfreethem))<br>
> > x)<br>
> ><br>
> > (foo (dontfreeme "Test"))<br>
> ><br>
> > Laurent<br>
><br>
> But the garbage collector can still move the string when collecting, so<br>
> protecting it from being freed isn't enough.<br>
><br>
> Isn't there some way to declare an object immovable?<br>
><br>
> -- hendrik<br>
><br>
><br>
> > ____________________<br>
> > Racket Users list:<br>
> > <a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/users</a><br>
><br>
> ____________________<br>
> Racket Users list:<br>
> <a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/users</a><br>
____________________<br>
Racket Users list:<br>
<a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/users</a><br>
</div></div></blockquote></div><br></div>