Libgc: why is this code leaking?

I tried using libgc (BDW garbage collector) inside this simple code.

Note that this link is only kept for the last node in the bogus "list", so the live set is only the last two nodes.

// thanks to @chill for this example
#include <gc.h>

struct list {
    struct list* next;
};

int main() {
    GC_INIT();
    struct list *last = NULL;
    for (;;) {
        struct list* nuo = GC_MALLOC(sizeof(struct list));
        nuo->next = NULL;
        // if next line is commented, then no leakage
        if (last) last->next = nuo;
        last = nuo;
    }
}

      

But it cannot stay within memory:

$ gcc -O0 gc.c -lgc -o gc

$ GC_MAXIMUM_HEAP_SIZE = 100000000./gc

GC Warning: Out of Memory!  Trying to continue ...
GC Warning: Out of Memory!  Trying to continue ...
GC Warning: Out of Memory!  Trying to continue ...
GC Warning: Out of Memory!  Trying to continue ...
GC Warning: Out of Memory! Heap size: 95 MiB. Returning NULL!
Segmentation fault

      

How am I wrong? Ubuntu 15.04 x86_64 gcc 4.9.2 libgc 7.2d-6.4

Update: I just compiled the trunk version from https://github.com/ivmai/bdwgc and it looks correct. So the bug is only in 7.2d or in the version packaged in Ubuntu.

Update: libgc 7.2f compiled from source also works correctly. So this is just a problem with the Ubuntu and Debian version.

+3


source to share


2 answers


It might be a bug, but it might be the victim of a false pointer. BDWGC is a conservative GC; if the word "looks" like a pointer to GC_malloced memory, the memory is saved. If some kind of false pointer hits one of your node lists, it will be accidentally saved and all nodes pointing to it will be saved as well.

Discussed in terms of weak GC stability. For more information see the following document:



http://www.hpl.hp.com/techreports/2001/HPL-2001-251.pdf

A common idiom is to manually change the next link when the node is not in use.

+3


source


Since you are allocating memory in an infinite loop.



0


source







All Articles