R
Retep
Hi all,
hopefully someone can answer the following mystery for me:
I have the following (simplified) application setup:
Main Form
|
Form
|
Controller
|
Entity
A Form owns a Controller, a controller owns an Entity (the business object)
Forms are created from the main form.
I have a test setup where my Main Form contains a button to create Forms
(the variable is created locally on method level).
The Form creates a controller in its constructor (and holds this reference
in a private field on class level) and the controller creates an Entity in
its constructor(and holds the reference on class level).
If have created a finalizer in the Entity class that outputs the string
"GCed" to the console.
On the Main Form there's also a button that explicitly calls GC.Collect()
(just for testing purposes).
In my test scenario, I create several Form objects that I subsequently close
using the X button in the titlebar.
I would expect that the objects (Form, controller and Entity) that are now
no longer reachable, would be collected by the GC (which would call the
finalizers of the entities) but this doesn't happen. No matter how many
Forms I create and close and call GC.Collect(), the text "CGed" does not
show up in the console. Only when I close the application itself, I get the
texts "CGed" for each Entity in the console.
Could someone shed some light on why the GC does not collect these objects?
Does the main form somehow keeps referemces to the created Forms, even
though the variable I used to create it was declared locally on method
level?
Any thoughts are appreciated!
hopefully someone can answer the following mystery for me:
I have the following (simplified) application setup:
Main Form
|
Form
|
Controller
|
Entity
A Form owns a Controller, a controller owns an Entity (the business object)
Forms are created from the main form.
I have a test setup where my Main Form contains a button to create Forms
(the variable is created locally on method level).
The Form creates a controller in its constructor (and holds this reference
in a private field on class level) and the controller creates an Entity in
its constructor(and holds the reference on class level).
If have created a finalizer in the Entity class that outputs the string
"GCed" to the console.
On the Main Form there's also a button that explicitly calls GC.Collect()
(just for testing purposes).
In my test scenario, I create several Form objects that I subsequently close
using the X button in the titlebar.
I would expect that the objects (Form, controller and Entity) that are now
no longer reachable, would be collected by the GC (which would call the
finalizers of the entities) but this doesn't happen. No matter how many
Forms I create and close and call GC.Collect(), the text "CGed" does not
show up in the console. Only when I close the application itself, I get the
texts "CGed" for each Entity in the console.
Could someone shed some light on why the GC does not collect these objects?
Does the main form somehow keeps referemces to the created Forms, even
though the variable I used to create it was declared locally on method
level?
Any thoughts are appreciated!