reference to delegates and garbage collection
AnFive via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Jul 14 11:33:31 PDT 2014
Hello everybody. I am new to D, and I am trying to familiarize
with all the new (to me) features.
I have a question about a strange behavior I cannot understand.
(In case it matters, I am using gdc 2.065 for Windows, but the
same happens with dmd).
Example code :
class A {
void delegate() del;
~this() { writeln("A Destructor called"); }
}
class B {
void fun() {}
}
A makeA() {
auto a = new A();
a.del = &(new B().fun);
return a;
}
void main() {
auto a = makeA();
/* Magic line goes here, see below */
a = null;
foreach(i; 0..10) {
writeln("Collecting");
GC.collect();
}
writeln("End");
}
If I run this code, the instance of A is of course collected at
the first call to GC.collect().
But if I call the delegate (i.e. a.del() ) in the "magic line",
the instance is not destroyed until the end of the main. If I
"wrap" the call to a.del() in another function or method, the
instance is collected.
Can somebody explain why this happens? Is it just a strangeness
of the GC? Does calling the delegate keep a reference to a in the
current scope? Am I going crazy? I spent about half an hour
looking for a non-existent memory leak in my program because of
this behavior, so I'd like to have an explanation.
Thanks in advance!
More information about the Digitalmars-d-learn
mailing list