Hi Marina,
Let me illustrate the situation a little better:
---------------------------------
Module MyModule
....
Public MyObjectA As MyObjectTypeA
Public MyObjectB As MyObjectTypeB
....
Public Sub Main()
Dim MyObjectC As New MyObjectTypeC
Debug.WriteLine("Main:About to initialize ObjC")
MyObjectC.Initialize()
Debug.WriteLine("Main:About to Process ObjB")
MyObjectB.Process()
Debug.WriteLine("Main:About to go into message loop")
Application.Run()
End Sub
-----------------------------------
Class MyObjectTypeC
....
Public Sub Initialize
Debug.WriteLine("ObjC_Init:About to instantiate ObjB")
MyObjectB = New MyObjectTypeB
End Sub
------------------------------------
Class MyObjectTypeB
....
Public Sub Process
Debug.WriteLine("ObjB_Process:About to instantiate ObjA")
MyObjectA = New MyObjectTypeA
End Sub
------------------------------------
WndProc
....
If e.Msg = TYPE_A_PROCESS_MSG
Debug.WriteLine("WndProc:About to process Obj A msg")
MyObjectA.ProcessMsg()
End If
------------------------------------
Class MyObjectTypeA
....
Public Sub Finalize()
Debug.WriteLine("ObjA:Garbage collection cleaning me up!")
End Sub
Public Sub ProcessMsg()
Debug.WriteLine("ObjA:About to do stuff")
DoStuff()
Debug.WriteLine("ObjA:About to force GC")
GC.Collect()
Application.DoEvents()
End Sub
------------------------------------
This would be the output based on the above:
Main:About to initialize ObjC
ObjC_Init:About to instantiate ObjB
Main:About to process ObjB
ObjB_Process:About to instantiate ObjA
Main:About to go into Message loop
....
[Somebody sends us a TYPE_A_PROCESS_MSG]
WndProc:About to process Obj A msg
ObjA:About to do stuff
ObjA:About to force GC
ObjA:Garbage collection cleaning me up!
-------------------------------------
That's what's happening. For some reason, GC thinks Object A has no
references. I'm baffled.
-Mike
"Marina" wrote:
What makes you think that garbage collection is getting rid of the object?
Or that it is even running whent his happens?
This is the first I've heard of this kind of thing. If anything, the
opposite is usually the problem. I tend to think there is something else
going on.
"mike2036" <mi******@discussions.microsoft.com> wrote in message
news:D8**********************************@microsof t.com... For some reason it appears that garbage collection is releasing an object
that I'm still using. The object is declared in a module and instantiated
within a class that is in turn instantiated by the mainline. The class
that
instantiated the object in question is definitely still in existence at
the
point garbage collection swoops in and yanks it out from under my
processing.
Is there a way to ensure an instantiated object cannot be freed by garbage
collection until I want it to? Also, if there is a definitive description
of
the rules of garbage collection that you could direct me to, that would be
appreciated. Thanks for any help.
-Mike