[racket] make-sized-byte-string and GC

From: Ryan Culpepper (ryanc at ccs.neu.edu)
Date: Sat Jan 25 14:37:30 EST 2014

On 01/25/2014 01:28 PM, Roman Klochkov wrote:
> Is making bytestring from pointer adds the pointer to GC?
> 
>
>  > (define x (malloc 'raw 10))
>  > x
> #<cpointer>
>  > (define b (make-sized-byte-string x 10))
>  > (cpointer-gcable? b)
> #t
>  > (cpointer-gcable? x)
> #f
>  > (cast x _pointer _int32)
> 173726656
>  > (cast b _pointer _int32)
> 173726656
>
> So b and x points to the same block of 10 bytes, but value of b is
> GCable and value of x is not.
> I assume, that when b will be changed, then the bytestring will be
> collected and accessing x will give segfault. Am I right?

I think it's a bug that (cpointer-gcable? b) returns true, since the FFI 
generally treats bytestrings as pointers to the memory that stores their 
contents, and in this case that memory is not managed by the GC.

The bytestring object itself (which consists of a header, a pointer, and 
a length, IIRC) is collectible, but then so is the cpointer object 
(which consists of a header, a pointer, and some other stuff, like a tag 
list).

So no, you should not expect a segfault. On the other hand, if you free 
x and use b afterwards, then you should expect a segfault or some other 
form of memory corruption.

Ryan



Posted on the users mailing list.