I often declare/define an Empty() member function for my classes, to make sure it's in a known
state with all held resources released.
I call this Empty() member in my constructors and destructors and throughout the program.
However, I've been recently experiencing weird GPFs, programs just exiting abruptly, and pointer-incrementing not working correctly (!). These faults were eventually traced to having the Empty() call in my constructors; removing the Empty() call (or making it an inline) made the program work correctly.
I'm aware that a general Empty() won't work inside a constructor because if you have something like:
if (ptr!=NULL) free(ptr) inside your Empty() and that ptr (member of MyClass) is set to a random value by the time the constructor is called, freeing it is a bit silly.
However, I am very careful to make sure that I initialise all such pointers that will be used by the Empty() as NULL in the constructor before the Empty() is called. nevertheless, I stil get weird happenings.
Is this an MFC-specific thing, or should I generally never use member functions inside constructors, on the basis that the object isn't fully constructed yet ?
If so, what's the best strategy for non-duplication of code between the "construct" and "empty" functions when you have a lot of member variables that need to be initialised ? Having to write out what amounts to the same code in the two cases is a bit excesive.
Richard [in SG1]
// arbitrary data members
// .. Set various resource holders to NULL.f
// release resources.