[plt-scheme] MzScheme extension

From: Chongkai Zhu (u0476504 at utah.edu)
Date: Mon May 29 09:11:37 EDT 2006

----- Original Message ----- 
From: "Ivanyi Peter" <pivanyi at freemail.hu>
To: "pltscheme" <plt-scheme at list.cs.brown.edu>
Sent: Monday, May 29, 2006 6:33 PM
Subject: [plt-scheme] MzScheme extension


>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.

>- 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.

>Generally what I am concerned about, that I do not want to leak 
>any memory in the extension. (However I have variable length
>data structures.)

The default GC of mzscheme works well engough in geneal cases. But 
it do leak memory in some cases. So I suggest you to use both malloc and 
GC in your program. It should work well. If later it turns out there is 
huge memory leak, you will have to use mzscheme3m instead.

>Thanks for any help.
>
>Peter Ivanyi
>

Chongkai

Posted on the users mailing list.