core: Unset device ctx if it has been destroyed

Devices can outlive their context in some cases (in particular with
python garbage collection). Guard against this by clearing the ctx
pointer so that it is not pointing to uninitialized memory.

Closes #1058
2 files changed