Use smart pointers in the context class
Fix a possible crash when deactivating contexts

Deactivating a context might try to activate a context that no longer
exists. Fix this by removing contexts from the list on their destructor.