I am trying to eliminate all memory leaks from some messy code. I have a class Employee with the member name_ which gets allocated on the heap with a new *char during runtime. Now there are some instances where name_gets deleted manually before Employee's destructor gets called. I know this is bad practice. However, there are some cases where I still need name_ to be deleted through the destructor. I tried things like
Employee::~Employee(){
if (this->name_)
{
delete[] this->name_;
}
}
or
if (this->name_ != NULL)
{
delete[] this->name_;
}
or
if (this->name_[0] != '\0')
{
delete[] this->name_;
}
But those options didn't realize name_ has already been delted somewhere else and tried to delete something which didn't exist anymore.
So I would need something like
if (char* name_ not deleted yet)
{
delte name_
}
Is there a way to check withing an if weather a member has already been deleted?
Edit: Thanks for the link to the question that already answered mine, indeed it does!