Your use of Delete() results in a dangling pointer. Here is what you are doing:
- You create a smart pointer.
- You assign an object called to the smart pointer.
- You call Delete() on the object. Now the smart pointer is dangling, because it points to an object that has been destroyed.
- You set the smart pointer to “nullptr”, which causes the smart pointer to call Delete() on the object again (via the dangling pointer). This causes a segfault.
The solution is simple. Do not destroy an object that is held by a smart pointer. To destroy the object, you should either assign a new value to the smart pointer (e.g. nullptr), or allow the smart pointer to destruct.