Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / July 2003

Tip: Looking for answers? Try searching our database.

Events: Do they lead to memory leaks?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rahul Kumar - 25 Jul 2003 10:09 GMT
Hi all

I have the following scenario in brief: There are two classes EventRaiser
and EventConsumer. The EventRaiser raises an event and EventConsumer listens
to it, by adding its handler to the event chain of the EventRaiser's event
delegate. My question is: If the EventConsumer goes out of context, without
removing itself from the event chain, does the EventRaiser still have its
reference through the event chain? Will it not be garbage collected?

Regards

RK
David Browne - 25 Jul 2003 15:16 GMT
> Hi all
>
[quoted text clipped - 4 lines]
> removing itself from the event chain, does the EventRaiser still have its
> reference through the event chain?

Yes

>Will it not be garbage collected?

It will not be garbage collected.

David
Rahul Kumar - 25 Jul 2003 16:58 GMT
Hi David

Thanks for the response. I have got few more questions for you:

How to take care of the memory leaks in such cases?
Are there in safeguards in .Net against it, except good programming
practices?
How about using WeakReferences to link the two Event objects?

Regards

Rahul

> > Hi all
> >
[quoted text clipped - 14 lines]
>
> David
David Browne - 25 Jul 2003 17:35 GMT
> Hi David
>
[quoted text clipped - 4 lines]
> practices?
> How about using WeakReferences to link the two Event objects?

Well that's not how events are implemented, but they could have been.

If you used interfaces instead of events, you could use weak references to
notitify the object without keeping it from being collected.

But only good programming prevents this.  You might make your objects
Disposable, and remove the event handler in the Dispose method.

David
Niall - 28 Jul 2003 01:38 GMT
Weak references could have worked in this situation. Bear in mind that it's
only really an issue when your object is hooking onto an event in another
object which has a significantly longer life span. But it can be a really
nasty situation, especially if it's not you hooking up the event, but
something in the framework, and your object ends up being referenced through
a static hashtable or similar.

Niall

> Hi David
>
[quoted text clipped - 30 lines]
> >
> > David

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.