Garbage collection

G

Guest

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
 
M

Marina

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.
 
G

Guest

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
 
M

Marina

Are you trying to say that object A forces its own garbage collection?
That's what it seems you are trying to say.

Also, if you run this twice, the second time MyObjectA.ProcessMsg() is
called, is there an error?

Also, I am not sure why you are trying to force garbage collection? Why not
let it happen when deemed necessary by the run time?

mike2036 said:
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 said:
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.
 
G

Guest

I force garbage collection as a test, not as a normal activity. Without it
it still happens, but as an automated occurence and therefore not
consistently. When I force garbage collection, it happens every time.

Marina said:
Are you trying to say that object A forces its own garbage collection?
That's what it seems you are trying to say.

Also, if you run this twice, the second time MyObjectA.ProcessMsg() is
called, is there an error?

Also, I am not sure why you are trying to force garbage collection? Why not
let it happen when deemed necessary by the run time?

mike2036 said:
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 said:
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.

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
 
C

Chris Dunaway

What does "DoStuff" do? Does it interact with any of the A, B, or C
objects?
 
B

Brian Gideon

Mike,

Can you post a short, but complete program that demonstrates the
problem?

Brian
 
G

Guest

Hi Brian,

I did build an applet to attempt to recreate the problem but have been
unsuccessful. In my "DoStuff()" routine I interface with one of our DLL's,
and I suspect it's messing with memory or doing something it shouldn't be,
which is triggering Garbage Collection.

Brian Gideon said:
Mike,

Can you post a short, but complete program that demonstrates the
problem?

Brian
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top