Thursday, September 2, 2010

WCF client side proxy memory leaks

Today I ran into a guy who worked with me on my previous project.
On that project I was responsible to rewrite the WCF channels (proxies) generation management and lifetime management of them.
I decided to do some research and found some good stuff here.
So this guy (aka Kurki) tells me they found a memory leak in my manager, what was the problem?

So i messed up like I always do (well, only 90% of the time, like a good programmer).

I wrote a code that registers to the Faulted event of the channel, kills the channel when Faulted and creates a new one to work with, everything looked fine, only one screw up (I'm sure some of you can guess).
I forgot to unregister from the Faulted Event.

On the web they tell you to do some unique logic to kill your channel in a safe way, what they don't talk about is that it'll stay in memory and fire events if you won't unregister from stuff although you think it is dead.

So please do not forget to unregister from stuff even if the object is disposable.

Or consider the use of weak event listeners and problem solved.