[gclist] Segmentation faults

Fergus Henderson fjh@cs.mu.oz.au
Wed, 2 May 2001 02:15:56 +1000


On 01-May-2001, elmex@x-paste.de <elmex@x-paste.de> wrote:
> i am working on a interpreter for a new scripting language.
> My OS is linux-2.4.x and i use the Boehm GC 4.14.
> The interpreter uses dlopen to load modules (which use the
> gc-malloc too (gc.a is linked)).

If you are using dlopen(), then probably the modules and the interpreter
should both be linked with a shared version of the GC library (libgc.so),
rather than a static version (gc.a).  Otherwise you will end up with
two copies of the garbage collector in your application.
This is wasteful, and may also cause problems, e.g. each copy
may not scan memory allocated with the other copy.

> But there are modules which use the normal mallocs too (eg. from libwww).

You need to make sure that you do not store pointers to memory
allocated with GC_malloc() in space allocated with malloc().
See README.QUICK:

 | WARNINGS:
 | 
 | Do not store the only pointer to an object in memory allocated
 | with system malloc, since the collector usually does not scan
 | memory allocated in this way.

> I had other problems with overwritten data in my parse tree before.
> I thougt that was a problem with type-casting a pointer to 
> big structure to a small structre, and the GC overwrote the data after the 
> small structs memory.
> Here is what i mean:
> ===
> struct L { char *a; char *b }; struct S { char *a; };
> ...
> struct L *ptr=malloc(sizeof(struct L));
> struct S *sptr=NULL;
> struct L *lptr=NULL;
> ptr->a = GC_malloc(sizeof(char) * 4); ptr->a = "foo";
> ptr->b = GC_malloc(sizeof(char) * 4); ptr->b = "bar";

This is a bug: you are storing pointers to memory allocated
with GC_malloc() in space allocated with malloc().

Of course, avoiding these kinds of bugs takes care.
The other alternative is to use -DREDIRECT_MALLOC=GC_malloc.
See the documentation in the Makefile.

-- 
Fergus Henderson <fjh@cs.mu.oz.au>  |  "I have always known that the pursuit
                                    |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.