views:

160

answers:

4

I'm using C# .NET 3.5 ... and I've been working to decouple a BLL object by moving database related activity into a seperate worker object. The worker object adds entities to the database and events a success or failure message back to a BLL object.

When I instance the worker object in the BLL I wire up the worker's events and set the BLL's event handler using the event += delegate(eventhandler) syntax.

I've heard that if I don't explicitly unwire the listeners with the -= syntax when the worker is disposed that there is a potential for memory leaks.

All of this processing occurs in a windows service that picks up messages from a queue and calls the appropriate BLL object ... I'm worried that I might be introduce a memory leak into this process.

+1  A: 

If you only ever subscribe to the event once in your service, you should be fine.

Oded
+2  A: 

Subscribing to an event adds a reference from the subscriber to the provider.

x.Event += y.handler means that x now holds a reference to y

If x has a longer lifespan than y then y cannot be garbage collected until all references to x have disappeared.

In your case you are listening to events from the workers in the BLL (if I understand you correctly) then you have references to the workers remaining in the BLL unless you explicity unsubscribe.

However, if you are finishing with the worker at the same time as the BLL then it won't actually matter.

Matt Breckon
Yes this is correct, I'm listening to events from the worker in the BLL. Once the work is done both the BLL and the workers are both going away. The wireup is:worker.errorevent += new worker.error(BLLsMethod)What I was wondering is in the cleanup of the BLL I need to unwire this, but as you said since both the worker and the BLL are going away once this process is complete then I don't need to worry about this.
Loathian
+2  A: 

What you heard is true. As long as your object is subscribed to an event from another reachable object, the runtime will not release that object. If your application creates one or a small set of worker objects at the beginning for its lifetime, then you should be OK. If the application creates a worker object for each BLL object but the reference to BLL instance is released, you should be OK too.

The dangerous thing is to blindly subscribe an instance method to a static event or an instance event of an object with a long lifetime.

Mehrdad Afshari
A: 

Look into using a .NET background worker or thread pool and the synchronization mechanisms available with those solutions.

Firestrand
What? How does that have any relevance whatsoever?
Greg Beech
I believed the author was talking about a typical unit of work situation. In my experience most errors in worker synchronization the author is alluding to can be avoided by using the .NET background worker or thread pool effectively.
Firestrand
Sorry I should have clarified this ... I'm not creating a seperate thread to do the work and communicating back with events, I'm just creating a worker object in the main thread and telling it to go do work. Messages to be written to response output are evented back from the worker as it has no knowledge of how responses are handled. If there is a better technique for doing this (such as maybe passing in a reference of the BLL to the worker and then calling methods of the BLL from the worker then I would be interested to hear more information on this subject.)
Loathian