Delegate is left with a destroyed stack object

Maxim Fomin maxim at maxim-fomin.ru
Tue Oct 29 15:23:08 PDT 2013


On Tuesday, 29 October 2013 at 21:57:25 UTC, Ali Çehreli wrote:
> On 10/29/2013 02:41 PM, David Nadlinger wrote:
>
> > Closures follow the infinite lifetime model – the struct
> scope is never
> > left, if you want.
>
> Agreed but the implicit nature of things is troubling. Imagine 
> that the program depends on the destructor to be called:
>

It may be confusing but I agree that disabling dtor invocation at 
the end of function scope is a right decision. Technically scope 
is left, but lifetime continues.

>
> > This is not exactly a new scenario, destructors on new'd
> structs aren't
> > called either (unless you manually destroy them).
>
> At least in the case of a new'ed structs I have a convenient 
> way to destroy them through a pointer. I think in the case of a 
> closure I must go through hoops to access or save that pointer.
>
> Ali

This is not a big deal.

Another but similar case:

import std.stdio;

struct S
{
	int i;
	~this() { writefln("dtor %X, %X", &this, i); }
}

auto foo()
{
	S s = S(1);
	struct SS
	{
		void bar() { s = S(2); }
	}
	return SS();
}

void main()
{
	foo().bar();
	
}

So, one need also to be aware of nested structs (or functions) 
touching local objects.


More information about the Digitalmars-d mailing list