Is this a bug or feature?

Li Jie cpunion at gmail.com
Mon Jan 15 23:57:52 PST 2007


Code:

# import std.gc;
#
# class Foo{
#   private:
#   int value = 3;
#
#   public:
#   ~this(){
#       writefln("call destructor");
#   }
#
#   void delegate() foo(){
#       return delegate void(){
#           writefln(value);
#       };
#   }
# }
#
# void main(){
#   void delegate() dg = (new Foo).foo;
#   std.gc.fullCollect();
#   dg();
# }

I expect it output "3", but the output is a big number, not "3".

If comment std.gc.fullCollect(), it output "3", and it is right.

I think a closure must save all contexts, isn't it?


More information about the Digitalmars-d-learn mailing list