Weak References

Steven Schveighoffer schveiguy at yahoo.com
Thu Aug 7 07:38:02 PDT 2008


"Bill Baxter" wrote
> On Thu, Aug 7, 2008 at 8:12 AM, Jason House wrote:
>> A weak reference library exists but doesn't always work...
>
> By doesn't always work you mean that it seems to you that sometimes it
> prevents the target object from being collected, right?
>
> But I think you said your only test case for this was something that
> is not reproducible, correct?
>
> Very frustrating to not be able to say for sure.  It would be a huge
> help if the GC exposed some API for querying where a particular
> pointer is being referenced.  Without that it's really hard to say
> where the reference actually is coming from.
>
> One possible gotcha that just crossed my mind:  I think the current GC
> allocates chunks in an all or nothing manner -- hasPointers is just a
> single bit.  So could it be that the fact that all Objects have a
> vtable pointer mean that everything in an Object is always treated as
> having pointers?

At least on Tango, this is not the case:

import tango.core.Memory;

class X
{
    int x;
}

class Y
{
    int *y;
}

void main()
{
    auto n = new X;
    assert(GC.getAttr(cast(void*)n) & GC.BlkAttr.NO_SCAN);
    auto m = new Y;
    assert(!(GC.getAttr(cast(void*)m) & GC.BlkAttr.NO_SCAN));
}

-Steve 





More information about the Digitalmars-d mailing list