This is what is happening.
Set ChildClass = ParentClass.GetChildClass(10)
In this line, ParentClass creates a new class and stores its reference into the local
Private ChildClass As Class1
And passes its handle to Main where it is stored in (Main):
Dim ChildClass As Class1
So, in reality there is only one class, but with two handles, and therefore its referenceCount is 2 (remember that a
VB class is a COM class)
When in the main you set
Set ParentClass = Nothing
ParentClass 'tries' to destroy ChildClass in the Terminate:
Set ChildClass = Nothing
but in fact what the above statement does is to decrease the class referenceCoount by 1. Because it was 2, it does not goes to zero (even at this age I can still make 1 plus 1, most of the time ;) therefore that class is not destroyed.
One question: why do you need a copy of ChildClass in childCLass? Oh, sorry, I guess that is a sample code, never mind that. Anyway, the trick is to avoid keeping unnecessary handles of the same class, otherwise it is hard to kill it. Even worse, a circular reference created in an ActiveX component will stop the application to close.
Let me know how it goes.
Marco