Never called destructor
bearophileHUGS at lycos.com
Fri Mar 26 12:20:18 PDT 2010
> This is clearly wrong, we are accessing a deleted object, and for some
> reason we aren't getting a double delete of y, which we should.
Thank you for the nice example, I think it's doing the struct return optimization trick invented by Walter ages ago :-)
"scope" for objects is a very useful optimization, I can show you benchmarks, because DMD isn't able to perform escape analysis as recent HotSpot does. But I think Walter knows it's not safe (it can be made quite more safe if more sanity tests are added to DMD, but this decreases their flexibility and usefulness when you need more performance).
So don't use it to increase code safety, because it fails at that. Use it only where you can experimentally see a performance improvement. And where possible don't copy, reassign or return the reference of a scoped object :-) Objects in D are not meant to be copied. And the purpose of a scoped object is to exist only in a scope, to avoid a heap allocation. If you try to break free of this semantics you are on your own.
Do you want to write a bug report? I fear that the only way to make scoped objects fully semantically sound is to remove them from the language, and Walter was about to do it. But there are legit usages for scoped objects.
More information about the Digitalmars-d-learn