By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,115 Members | 894 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,115 IT Pros & Developers. It's quick & easy.

Garbage collection

P: n/a
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

Dec 8 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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

Dec 8 '05 #2

P: n/a
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


Dec 8 '05 #3

P: n/a
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" <mi******@discussions.microsoft.com> wrote in message
news:96**********************************@microsof t.com...
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
>


Dec 8 '05 #4

P: n/a
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" wrote:
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" <mi******@discussions.microsoft.com> wrote in message
news:96**********************************@microsof t.com...
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
>


Dec 8 '05 #5

P: n/a
What does "DoStuff" do? Does it interact with any of the A, B, or C
objects?

Dec 8 '05 #6

P: n/a
Hi Chris,

DoStuff() does not interact with any of the other objects.

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

Dec 8 '05 #7

P: n/a
Mike,

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

Brian

mike2036 wrote:
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


Dec 8 '05 #8

P: n/a
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" wrote:
Mike,

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

Brian

mike2036 wrote:
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


Dec 9 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.