Code:
class cClass
{
private:
struct sNode
{
sNode* Next;
};
sNode* First;
public:
cClass()
{
First = NULL;
}
~cClass()
{
sNode* tmp;
while( First != NULL )
{
tmp = First;
First = tmp->Next; // ***
delete tmp;
}
}
};
class cBiggerClass
{
private:
cClass* myclass;
public:
cBiggerClass()
{
myclass = new cClass;
}
~cBiggerClass()
{
delete myclass;
}
In this small test run, cBiggerClass is instantiated once and almost immediately destroyed afterwards. When that happens, cClass also gets destroyed, without any nodes having been created in the list. The problem is right then I get an exception at the *** line for an invalid address pointer on First. Apparently in cClass's destructor, the loop is actually being entered and it tries to figure out where First->Next goes, when of course First hasn't been initialized.
Through some experimentation with break points, it seems like cClass's constructor does not get called. Thus First seems to point to a random place, causing it to enter the loop. The debugger says First->Next is undefined when the error occurs.
Anyone have any idea why?

