Garbage collection from associative arrays
Sebastian Beschke
s.beschke at gmx.de
Sat Mar 25 05:47:08 PST 2006
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?
Regards, Sebastian
Output on my machine:
Loading...
class created
class created
class created
97
98
99
class killed (99)
class killed (98)
class killed (97)
Error: Access Violation
Program code:
private {
import std.gc;
import std.stdio;
}
class SomeClass {
this(char value) {
writefln("class created");
_value = value;
}
~this()
{
writefln("class killed (%d)", _value);
}
char value() {
return _value;
}
private {
char _value;
}
}
static char[] allChars = [
'a', 'b', 'c'
];
SomeClass[char] _chars;
void _realLoad() {
writefln("Loading...");
foreach(char ch; allChars) {
_chars[ch] = new SomeClass(ch);
}
}
int main(char[][] args)
{
bool done = false;
_realLoad();
while(!done)
{
foreach(char ch; allChars) {
SomeClass obj = _chars[ch];
writefln("%d", obj.value);
}
std.gc.fullCollect();
}
}
More information about the Digitalmars-d-bugs
mailing list