[plt-scheme] malloc/free inside MzScheme

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Mon Jan 9 10:50:33 EST 2006

At Sun, 08 Jan 2006 20:12:10 -0700, Chongkai Zhu wrote:
> Scheme_Env *e;
> 
> long write_errmsg_fun(Scheme_Output_Port *port, const char *buffer, long 
> offset, long size, int rarely_block)
> {
> 	int i, n;
> 	unsigned short *s;
> 	if (!(size == 0L || size==1L && buffer[offset]=='\12'))
> 	{
> 		s = (unsigned short *)malloc( scheme_utf8_decode( buffer, 
> offset, size, NULL, 0, -1, NULL, 1, 0));
> 		n = scheme_utf8_decode( buffer, offset, size, s, 0, -1, NULL, 
> 1, 0);
> 		for (i=0;i<n;i++)
> 			printf("%c", s[i]);
> 		free( s);//error here
> 	}
> 	return size;
> }

There's no guarantee that the bytes provided to a port's output
function are in UTF-8 encoded chunks, so you can't assume that
scheme_utf8_decode returns a non-negative result.

In your test, what is the value of `n'?

(I don't know if this is the problem you're seeing, but it's the first
thing I notice.)


FWIW, you also need a `MZ_REGISTER_STATIC(env)' before the call to
scheme_basic_env().

Matthew



Posted on the users mailing list.