[plt-scheme] MzScheme extension

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Mon May 29 12:38:42 EDT 2006

Some minor elaborations to Chongkai's answer...

At Mon, 29 May 2006 21:11:37 +0800, "Chongkai Zhu" wrote:
> >Hi,
> >
> >I have created an MzScheme extension. In the extension one of the
> >arguments of a function is a string. In the C code I write this:
> >
> >Scheme_Object *obj;
> >char *str;
> >
> >obj = scheme_char_string_to_byte_string(arg[2]);
> >str = SCHEME_BYTE_STR_VAL(obj);
> >
> >My questions:
> >
> >- I assume that obj is handled by MzScheme and I do not have to free
> >its memory, as the GC will do that once I return from the C function. 
> >Is that right?
> Yes, as long as you use the default GC.
> >
> >- Who "owns" the memory of str? Once I am done with the string
> >do I have to free it or it is also handled by the GC?
> Noone "owns" the memory of str. str is part of obj. So if you change things
> in str, obj (the Scheme string) will also be changed. If obj is freed, str is 
> freed too. Since the GC will handle obj, you don't need to free it.

This is not quite true; `str' is a GCable object itself, which means
that `str' may remain reachable (and allocated) even if `obj' is GCed.

> >- Another related question: Can I use malloc, calloc in the C fcuntions 
> >for my own data? What I mean that inside a function I allocate some
> >memory and before the function returns I also free it up. Is that OK
> >for MzScheme?
> Yes, that's OK for mzscheme.

But you must not store pointers to GCable objects in malloc()ed
objects, because they won't be visible to the collector.


Posted on the users mailing list.