Garbage collection from associative arrays

Oskar Linde olREM at OVEnada.kth.se
Sat Mar 25 06:40:55 PST 2006


Sebastian Beschke wrote:

> Sebastian Beschke schrieb:
>> The following program creates a couple of instances of a class and
>> stores them in an associative array. It then tries to periodically
>> access those instances via the associative array, performing garbage
>> collects in between.
>> 
>> As it appears, a fullCollect will destroy the objects, even though they
>> are stored in an associative array. This leads to an access violation in
>> the second loop.
>> 
>> Is this a known bug? Can it be fixed? Are there any workarounds?
>
> Well, I found a workaround: Just modify the index type of the
> associative array to int.

Yeah. The error lies in the AA implementation. The pointers are not stored
on the correct alignment. Each node of the AA is allocated in the following
way:

e = cast(aaA *) cast(void*) new byte[aaA.sizeof + keysize + valuesize];

The aaA struct contains two pointers (left&right) and a uint hash. That is
12 bytes on a 32 bit machine. If keysize isn't divisible by 4, the pointers
stored as values will not be 4-byte aligned.

/Oskar



More information about the Digitalmars-d-bugs mailing list