Whenever you are a new object, you need to delete it, freeing memory
ClassA* object = new ClassA(); delete object;
The point of setting pointers to NULL is to stop dereferencing invalid pointers
object = NULL;
This is to ensure that tests are performed before attempting dereferencing:
if(object != NULL) { object->SomeMethod();
Also note that you can remove memory from any pointer that points to it.
ClassA* object = new ClassA(); ClassA* pointer1 = object; ClassA* pointer2 = object; delete pointer1;
object, pointer1, and pointer2 now all point to already released memory, and if they are not redefined, they should all be set to NULL .
source share