[plt-scheme] Memory management and MzScheme

From: Anton van Straaten (anton at appsolutions.com)
Date: Sat Nov 23 17:22:15 EST 2002

> static Scheme_Object *scheme_test(int argc, Scheme_Object **argv)
> {
>     std::vector<Scheme_Object *> v;
>     for(int i =0 ; i < 10000; i++)
>         v.push_back(scheme_make_string ("test"));
>     return scheme_build_list (10000,&v[0]);
> }
>
> This build a list of strings. MzScheme displays the first elements
> of the list correctly but at some point the list is corrupted. I've
> replaced the offending code with:
>
> static Scheme_Object *scheme_test2(int argc, Scheme_Object **argv)
> {
>     Scheme_Object** result = (Scheme_Object**) scheme_malloc
> (sizeof(Scheme_Object*) * 10000);
>     for(int i =0 ; i < 10000; i++)
>         result[i] = scheme_make_string ("test2");
>     return scheme_build_list (10000,result);
> }
>
> which works fine. Using a stack alocated array also works while
> using new or malloc fails. What am I missing?

The obvious possibly problematic difference between your two examples is
that one is using scheme_malloc and the other is not.  I haven't actually
done any extension work with MzScheme specifically, but I'd guess that this
could lead to trouble.

(If I'm wrong, I trust that a PLT person will correct me...)

If that is the problem, it could presumably be corrected by implementing a
custom STL allocator which uses scheme_malloc (assuming something like this
isn't already provided by PLT).

Anton



Posted on the users mailing list.