<HTML><BODY>So, to make string, that is freed by reciever, I should make something like<br><br>(define _string/transfer <br> (make-ctype _bytes<div> (λ (v) </div><div> (define b (if (string? v) (string->bytes/utf-8 v) v))</div><div> (define length (bytes-length b))</div><div> (define res (malloc 'raw length))</div><div> (memcpy res b length))</div> (λ (v) (register-finalizer v free))))<br><br>Or finalizer will not work (because v is _pointer, but the type returns _bytes), and instead I have to make<br>(λ (v)<br> (define res (cast v _pointer _bytes))<br> (register-finalizer v free))<br><br>?<br><br><br>Суббота, 25 января 2014, 14:37 -05:00 от Ryan Culpepper <ryanc@ccs.neu.edu>:<br>
<blockquote style="border-left:1px solid #0857A6; margin:10px; padding:0 0 0 10px;">
<div id="">
<div class="js-helper js-readmsg-msg">
<style type="text/css"></style>
<div id="style_13906786910000000035" class="mr_read__body">
<base target="_self" href="https://e.mail.ru/">
<div id="style_13906786910000000035_BODY">On 01/25/2014 01:28 PM, Roman Klochkov wrote:<br>
> Is making bytestring from pointer adds the pointer to GC?<br>
> <br>
><br>
> > (define x (malloc 'raw 10))<br>
> > x<br>
> #<cpointer><br>
> > (define b (make-sized-byte-string x 10))<br>
> > (cpointer-gcable? b)<br>
> #t<br>
> > (cpointer-gcable? x)<br>
> #f<br>
> > (cast x _pointer _int32)<br>
> 173726656<br>
> > (cast b _pointer _int32)<br>
> 173726656<br>
><br>
> So b and x points to the same block of 10 bytes, but value of b is<br>
> GCable and value of x is not.<br>
> I assume, that when b will be changed, then the bytestring will be<br>
> collected and accessing x will give segfault. Am I right?<br>
<br>
I think it's a bug that (cpointer-gcable? b) returns true, since the FFI <br>
generally treats bytestrings as pointers to the memory that stores their <br>
contents, and in this case that memory is not managed by the GC.<br>
<br>
The bytestring object itself (which consists of a header, a pointer, and <br>
a length, IIRC) is collectible, but then so is the cpointer object <br>
(which consists of a header, a pointer, and some other stuff, like a tag <br>
list).<br>
<br>
So no, you should not expect a segfault. On the other hand, if you free <br>
x and use b afterwards, then you should expect a segfault or some other <br>
form of memory corruption.<br>
<br>
Ryan<br>
<br>
</div>
<base target="_self" href="https://e.mail.ru/">
</div>
</div>
</div>
</blockquote>
<br>
<br>--
<br>Roman Klochkov<br></BODY></HTML>