StrataFrame Forum
Home      Members   Calendar   Who's On
Welcome Guest ( Login | Register )
      



Garbage collection and manual interventionExpand / Collapse
Author
Message
Posted 06/20/2007 1:11:26 AM
StrataFrame Novice

StrataFrame NoviceStrataFrame NoviceStrataFrame NoviceStrataFrame NoviceStrataFrame NoviceStrataFrame NoviceStrataFrame NoviceStrataFrame Novice

Group: StrataFrame Users
Last Login: 12/18/2008 7:22:43 PM
Posts: 77, Visits: 635
G'day

I was just reading through the forum looking for some information on broken rules (found it!) when I came across the topic

http://forum.strataframe.net/Topic4577-6-1.aspx?

In this Trent states

... the number one killer of many .NET developers is not cleaning up after the code and expecting the garbage collector to do the work.  The other thing, that goes hand in hand here, are event handlers.  If you ever gain a handler reference to any object, not just a BO, it will stay there until you explicitly remove the handler.  ... you will definitely want to Dispose your manually created objects when you are through with them...whether it be a BO or some other type of object.

Previously I hadn't given much thought to garbage collection and disposing of objects, but I will certainly try to now.

I didn't quite follow what was said next about event handlers.

My understanding is that if in a form I have something like:

Private Sub btnRefresh_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRefresh.Click

Me.PopulateGrid1()

End Sub

This will be garbage collected  when the form (i.e. the class handling the event) exits.

Is this correct?

Could someone provide an example of an event handler that wouldn't be disposed, say when a form exits, and how and where it should be disposed if I did want to get rid of it when the form exits?

Peter

Post #9667
Posted 06/20/2007 9:07:03 AM


StrataFrame Developer

StrataFrame Developer

Group: StrataFrame Developers
Last Login: 12/09/2008 3:36:08 PM
Posts: 2,686, Visits: 1,891
In VB, if you use the Handles keyword, you don't have to worry about cleaning it up.  In fact, there's really not a keyword to remove an implicitly created handler like that...

However, if you ever use the "AddHandler obj.Event, AddressOf MethodHandler" to add an event that you didn't define WithEvents, you should remember to call the "RemoveHandler obj.Event, AddressOf MethodHandler" on it.


www.bungie.net
Post #9668
Posted 06/20/2007 8:09:06 PM
StrataFrame Novice

StrataFrame NoviceStrataFrame NoviceStrataFrame NoviceStrataFrame NoviceStrataFrame NoviceStrataFrame NoviceStrataFrame NoviceStrataFrame Novice

Group: StrataFrame Users
Last Login: 12/18/2008 7:22:43 PM
Posts: 77, Visits: 635
Thanks Ben

 I think I've got it now.

Peter

Post #9687
Posted 06/21/2007 9:38:35 AM


StrataFrame Developer

StrataFrame Developer

Group: StrataFrame Developers
Last Login: 12/09/2008 3:36:08 PM
Posts: 2,686, Visits: 1,891
Excellent


www.bungie.net
Post #9700
« Prev Topic | Next Topic »


Reading This TopicExpand / Collapse
Active Users: 0 (0 guests, 0 members, 0 anonymous members)
No members currently viewing this topic.
Forum Moderators: Ben Chase, Trent L. Taylor, Steve L. Taylor

PermissionsExpand / Collapse

All times are GMT -6:00, Time now is 12:46pm

Powered by InstantForum.NET v4.1.4 © 2009
Execution: 0.063. 13 queries. Compression Enabled.
Site Map - Home - My Account - Forum - About Us - Contact Us - Try It - Buy It

Microsoft, Visual Studio, and the Visual Studio logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries.