But that would make them a lot less useful.
Useful? Maybe. However, consistency is more important IMHO. Saving references to stack frames that have gone out of scope is really confusing since unrelated variables from the same frame will be alive as well. Consider this for example:
Thread.Mutex mutex = Thread.Mutex();
function get_get_n() { Thread.MutexKey some_key = mutex->lock(); int n = 17; return lambda() { return n; }; }
int main() { function get_n = get_get_n(); Thread.MutexKey another_key = mutex->lock(); werror("%O\n", get_n()); }
Wohoo, recursive mutex locks! For even more fun, replace get_get_n() with the following to take advantage of another known Pike deficiency where the mutex key doesn't even have to be part of the same frame to begin with. Talk about minefield...
function get_get_n() { int n = 17; { Thread.MutexKey some_key = mutex->lock(); // ...protected stuff... } return lambda() { return n; }; }