Exiting from 'foo' invalidates the anonymous class' link to 'status'. The correct way to do this is to have a direct pointer to the outer stack frame:
return &(new class
{
int *p_status;
this() { p_status = & status; }
int dg() {
return ++ *p_status;
}
}
).dg;