No, this code leaks the object that you allocated with new
.
If an object is allocated with new
, it must be deallocated with delete
.
What I'm asking is if a pointer should be deleted even if nullptr has been assigned to it.
The terminology that you use indicates that you are thinking about this the wrong way. It's not the pointer that you need to delete, rather the object. The pointer is just a way for your code to refer to the object. The pointer variable contains the address of the object. You can have multiple pointer variables that all refer to the same object. You do not delete the pointer, you delete the object.
Now, it might seem odd that you write the deallocation like this:
delete foo;
Naively you might think of that as meaning, delete the pointer foo. But it does not mean that. It means, delete the object whose address is stored in foo.