473,587 Members | 2,516 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Objects don't get Garbage Collected

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!


Jul 30 '06 #1
3 1793

"Retep" <Re******@chell o.nlwrote in message
news:ed******** *************** ****@news.chell o.nl...
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.
First check out

Smart Client Software Factory
http://msdn.microsoft.com/library/de...tml/scsflp.asp

Which is built on top of

Smart Client - Composite UI Application Block
http://msdn.microsoft.com/library/de...2/html/cab.asp

For a very capable and reasonably easy UI framework. I mention this because
you appear to be interested in promoting some MVC-type structure to your
project, and CAB is Microsoft's best guidance on how to do that in WinForms.
Also in the framework they have worked through the sometimes tricky
dependency and lifecycle issues in WinForms development.

>
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?
No, it does not. From what you have described. the forms should be
colledted. Event handlers are a common source of unexptected live
references, as Event sources hold refernces to all event listeners. If you
post a repro, someone will explain the behavior.

David
Jul 30 '06 #2
Retep,

Finalizers only need to be implemented when you hold onto resources that
need cleaning up (File handles, connections, sockets, etc).

In C#, the Finalize method performs the operations that a standard C++
destructor would do. if you supply a destructor you cannot predict when it
will be called - it will only be executed when the garbage collector runs and
removes it from memory. In actual fact, you can never guarantee that a
destructor will ever be called since we can tell the garbage collector NOT to
call an object's destructor with the SuppressFinaliz e method of the System.GC
class.

Above all, it doesn't make sense to attempt to "Print out a message" from
within a finalizer.

Peter


--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Retep" wrote:
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!


Jul 30 '06 #3
David,

Thanks for your response.

Your suggestion regarding event handlers lead me to the solution.
Turned out the Databindings that I used to bind controls to properties of
the Entity caused the objects to stay alive, not the event handlers.
This was easily solved by calling Databindings.Cl ear() for each databound
control in the Close event of the form.

R.

"David Browne" <davidbaxterbro wne no potted me**@hotmail.co mschreef in
bericht news:Ok******** ******@TK2MSFTN GP02.phx.gbl...
>
"Retep" <Re******@chell o.nlwrote in message
news:ed******** *************** ****@news.chell o.nl...
>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.

First check out

Smart Client Software Factory
http://msdn.microsoft.com/library/de...tml/scsflp.asp

Which is built on top of

Smart Client - Composite UI Application Block
http://msdn.microsoft.com/library/de...2/html/cab.asp

For a very capable and reasonably easy UI framework. I mention this
because you appear to be interested in promoting some MVC-type structure
to your project, and CAB is Microsoft's best guidance on how to do that in
WinForms. Also in the framework they have worked through the sometimes
tricky dependency and lifecycle issues in WinForms development.

>>
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?

No, it does not. From what you have described. the forms should be
colledted. Event handlers are a common source of unexptected live
references, as Event sources hold refernces to all event listeners. If
you post a repro, someone will explain the behavior.

David

Jul 30 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2597
by: Manlio Perillo | last post by:
Hi. This post follows "does python have useless destructors". I'm not an expert, so I hope what I will write is meaningfull and clear. Actually in Python there is no possibility to write code that follows C++ RAII pattern. Of course Python objects are not statics like in C++, but in C++ the auto_ptr class is used for enforcing this...
9
1911
by: F. Da Costa | last post by:
Hi, Does anybody know why IE5+ does *not* honour array objects (like a table) across a session? Example: Frame A contains a var tableVar which is set via form Frame B (on init) using top.A.tableVar = document.getElementById("someTable"); As long as Frame B is *not* 'refreshed/ reloaded' witk another page the
4
2135
by: Chuck Ritzke | last post by:
I keep asking myself this question as I write class modules. What's the best/smartest/most efficient way to send a large object back and forth to a class module? For example, say I have a data access module that creates a large disconnected dataset from a database. I want to pass that dataset back to the calling program. And then perhaps I...
4
1901
by: pachanga | last post by:
After you destroy a Object and its send to the garbage collections, can you retrieve the object back? Also, if you can, can you destroy an object permantly with no trace of it?
9
5814
by: Murat Ozgur | last post by:
Hello, I want to know about "creating new objects without assignment to a reference" like this : ... new Employee("John","Woo"); .... Is this a good programming practice ? How does garbage collector
1
1099
by: mirek | last post by:
Hi, I'd like to know how session variables are stored in the session. I if put a object to the session and then get it back, is this the same object or a new copy of it? The session holds references to the objects or eg. serialized byte stream? What about garbage collecting if the object lives in some moment only in session? There is no...
7
1562
by: gerry | last post by:
I have a com object that is used via an interop assembly - ie. I referenced the com object and vs.net created the interop .dll for it. I create one object per session when the page 1st loads and store it in the Session. I expected that the object would be released when the Session object was destroyed during at Session_End(). In my log I can...
11
3240
by: MikeT | last post by:
This may sound very elementary, but can you trap when your object is set to null within the object? I have created a class that registers an event from an object passed in the constructor. When my object is destroyed, I want my object to un-register this event. If I don't then the object would never be destroyed until the object I passed...
2
1056
by: =?Utf-8?B?WWFua2VlIEltcGVyaWFsaXN0IERvZw==?= | last post by:
I'm creating a lot if custom business objects. They work better, faster and lighter than using table adaptors. Totally cool so far except for one thing i just thought of: destructors. I've searched for this a little and can not find a good answer as to whether or not to create them nor how to use them. I kill all my datareaders (like a good...
0
7923
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8216
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8349
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8221
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6629
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5395
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2364
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1455
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1192
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.