A bug in my code

bearophile bearophileHUGS at lycos.com
Mon Sep 8 08:01:25 PDT 2008


Sergey Gromov, thank you for your explanations, you are quite clear, I have understood everything you have explained :-) And your code works.

I think this discussion may deserve to become a little tutorial useful for people that have never done such things (probably most people coming from all scripting languages, Pascal, Java and maybe C/C# don't know such things if the haven't seen in the University, so they are lot of people), it may be written here:
http://www.prowiki.org/wiki4d/wiki.cgi?DocComments/Phobos/StdGc


> addRange(&new_block.next, &new_block.data+1);

The trick of adding a range of pointers to pointers is cute :-)
I presume that +1 is necessary because it's an interval open on the right, even if the Phobos docs don't tell it:
void addRange(void* pbot, void* ptop);


>Both require Block.~this() to remove any unneeded roots or ranges<

Okay, I understand now. Jarrett Billingsley has told me that those ranges are inefficient, so I hope the roots are more efficient.

Using your second solution:
addRange(&new_block.next, &new_block.data+1);

I have added this destructor:

~this() {
    while (this.list_head != null) {
        Block* next_ptr = this.list_head.next;
        std.gc.removeRange(&this.list_head.next);
        std.gc.realloc(this.list_head.data, 0);
        std.gc.realloc(this.list_head, 0);
        this.list_head = next_ptr;
    }
}


Now I think I understand the Phobos GC API a bit better. But I'll have to be really careful, because doing mistakes with such things looks very easy still for me.

Bye and thank you,
bearophile


More information about the Digitalmars-d-learn mailing list