Auto objects and scope
Chris Nicholson-Sauls
ibisbasenji at gmail.com
Thu Nov 16 19:46:30 PST 2006
genie wrote:
> Forgive my ignorance, guys, but there is something in GC for D I
> can't spin my head about - look at this bit of code:
>
> Test tst=new Test;
>
> ....
> if(b)
> {
> auto Test t1=new Test;
> ...
> tst=t1;
> }
> ....
> tst.func();
>
> auto modifier forces the object to be deleted even though it was
> assigned to something else before leaving the scope - is this
> behaviour correct?
>
> Gene
Bill gave you a pretty complete answer, but I just wanted to point out you could do this
to prevent the deletion:
# if (b) {
# scope Test t1 = new Test;
# // ...
# tst = t1;
# t1 = null; // <-- this will prevent the deletion!
# }
-- Chris Nicholson-Sauls
More information about the Digitalmars-d
mailing list