Reviewing a quite old project I found the following curious code snippet (only relevant code extracted):
class CCuriousClass {
~CCuriousClass();
CSomeType* object;
};
CCuriousClass::~CCuriousClass()
{
while( object != NULL ) {
delete object;
}
}
Have I overseen anything or is it a plain road to undefined behaviour?
What I see here is that if object
is a null pointer at the point of CCuriousClass::~CCuriousClass()
being called everything will be fine - no action taken - but if object
is not null this will be an infinite loop with undefined behaviour inside.
Is this most likely a bug or some smart construct I don't understand?