Ensure we cannot switch to a deallocated ctx

This can occur when the context registers event handlers but is then
deallocated
Most of switch handling code
Revert "Ensure we cannot switch to a deallocated ctx"

This reverts commit aa789b863ee9890fad1992e0dc2abf8551a1ef9c.
Implements persistent events (closes #1)