I have a hashfactory that returns a dynamically allocated object.
Item * Class::foo()
{
int subscript = hash(someKey);
return factory[subscript]->create();
}
//function create() is an overridden function in an inherited class that returns Item *
Item *SomeClass::create()
{
return new SomeClass();
}
In order for me not to have memory leaks, do I have to ensure that every pointer that touches this ends up being assigned to NULL and the final pointer that is keeping track of it is deleted and then set to NULL?
It depends.
If this is your own class in your own project you are indeed responsible for deleting the memory that is allocated by you calling create() (or foo()).
If this is a library class you have created, and you have documented it well enough, the caller of your library code will be responsible for deleting the memory created by a call to foo().
In either way, you have to set things straight and either document or implement proper memory management.