struct dtor never called, what's wrong?
Steven Schveighoffer
schveiguy at yahoo.com
Fri Jun 15 15:10:54 UTC 2018
On 6/15/18 10:51 AM, ketmar wrote:
> Andrea Fontana wrote:
>
>> I hope I'm wrong. Maybe I'm missing something. Anyone can help?
>
> you are not wrong, this is bug in DMDFE.
Yep.
I tried using -vcg-ast, and see an interesting lowering:
for (; !__r115.empty(); __r115.popFront())
{
ref A r = __r115.front();
...
I believe that what happens is r really is a ref to... a temporary
return. Because the compiler at this point won't re-allocate that memory
to something else, it works, but this is actually a memory bug too!
The destructor likely is not added, because the AST doesn't show any
variable that needs it! r is a ref.
At the very least, the code should allocate a temporary and assign a ref
to that, so lifetime management works properly. I agree the better path
is to simply disallow ref, but this would be a non-code-breaking step
that would fix the issue of lifetime management.
Note that in D1, only opApply was the way to use foreach, and it
*requires* ref for the element, even if your foreach didn't use ref (and
even if the actual thing you are iterating didn't make sense as ref). It
was actually impossible to duplicate array foreach, which would not
allow you to ref the index. You'd have to create a temporary index, send
it into the delegate, and ignore any changes.
The lowering for ranges must have tried to simulate this ability
(allowing you to use ref or non-ref despite what the actual range
provides), but this kludge doesn't seem to be implemented properly.
-Steve
More information about the Digitalmars-d
mailing list