[plt-scheme] Memory management and MzScheme
On Sat, 23 Nov 2002, Pedro Pinto wrote:
> I am trying to build an extension for MzScheme on WindowsXp/Visual Studio
> 7.0. I seem to be unable to figure out what is wrong with:
>
> 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]);
> }
Are you loading into DrScheme or MrEd, and not just MzScheme?
Could it be that C++ is new'ing space for v? If so, that might
be your problem, because MrEd replaces the new operator so that
its C++ objects are garbage collected. Eventually, your C++ objects
will get garbage-collected too! That was our experience in linux.
We linked our C++ extension with a copy of new/delete that uses
malloc/free, so it wouldn't get linked at loadtime with MrEd's new/delete.
Ron Stanonik
stanonik at cogsci.ucsd.edu
/*
MrEd replaces the new operator so that C++ objects are garbage collected.
Eventually, your C++ objects will get garbage-collected too!
Compile
gcc -g -I/plt200src/include -c gcdemo.c
gcc -g -shared -o gcdemo.so gcdemo.o /plt200src/lib/mzdyn.o
Start mred
(load-extension "gcdemo.so")
(arf) - returns 3
do some scheme
(collect-garbage)
(arf) - returns 0
Or start drscheme
(load-extension "gcdemo.so")
(arf) - returns 3
(arf) - returns 3
(arf) - returns 3
hit the GC button
(arf) - returns something other 3
*/
#include "escheme.h"
class arf {
public:
int x;
arf();
};
arf::arf()
{
x = 3;
}
arf *arfp;
static Scheme_Object *sch_arf(int argc, Scheme_Object **argv)
{
if (!arfp) arfp = new arf;
return scheme_make_integer(arfp->x);
}
Scheme_Object *scheme_module_name()
{
/* This extension doesn't define a module: */
return scheme_false;
}
Scheme_Object *scheme_reload(Scheme_Env *env)
{
Scheme_Object *proc;
proc = scheme_make_prim_w_arity(sch_arf, "arf", 0, 0);
scheme_add_global("arf", proc, env);
return scheme_make_string("Hello Pad!");
}
Scheme_Object *scheme_initialize(Scheme_Env *env)
{
return scheme_reload(env);
}