Behavior of "auto"

Sean Kelly sean at f4.ca
Thu Dec 6 16:01:48 PST 2007


bearophile wrote:
> To experiment I have modified your code some (this tread probably belongs to D.learn):
> 
> import std.gc;
> 
> class Test {
>     int i;
>     this(int i) {
>         this.i = i;
>         printf("%d created\n", this.i); }
>     ~this() { printf("%d destroyed\n", this.i); }
> }
> 
> void main() {
>     printf("Part 1 ----------\n");
>     for (int i = 0; i < 5; i++)
>         auto t = new Test(i);
>     printf("Part1 end ----------\n\n");
> 
>     printf("Part 2 ----------\n");
>     for (int i = 0; i < 5; i++)
>         auto scope t = new Test(i+5);
>     printf("Part 2 end ----------\n\n");
> 
>     printf("Part3 ----------\n");
>     foreach(i, _; new byte[5])
>         auto scope t = new Test(i+10);
>     printf("Part 3 end ----------\n\n");
> 
>     printf("Part4 ----------\n");
>     foreach(i, _; new byte[5])
>         auto t = new Test(i+15);
>     printf("Part 4 end ----------\n\n");
> 
>     std.gc.fullCollect();
> }
...
> 
> Part3 ----------
> 10 created
> 11 created
> 12 created
> 13 created
> 14 created
> Part 3 end ----------
...
> 
> I don't see where the 10-14 objects are deallocated (I have added a fullCollect, but it just scrambles the order of the those last deallocations).

I'd guess that this is a bug, and is related to the extra work the 
compiler does for foreach loops.


Sean



More information about the Digitalmars-d mailing list