After reviewing a rather old project, I found the following curious piece of code (only selected code):
class CCuriousClass { ~CCuriousClass(); CSomeType* object; }; CCuriousClass::~CCuriousClass() { while( object != NULL ) { delete object; } }
Am I in control or is this a simple way to undefined behavior?
What I see here is that if object is a null pointer at the point CCuriousClass::~CCuriousClass() being called, everything will be fine - no action is taken, but if object not null, it will be an infinite loop with undefined behavior inside.
This is most likely a mistake or some kind of smart design, which I do not understand?
source share