Detach Your Event Handlers
If you would ask any number of .NET developers with a native programming background about the biggest benefits of the platform, they’ll most likely bring up its automatic memory management capabilities that relieve developers of the burden of manual bookkeeping.
As good as this might sound, there are still some caveats to be aware of. In particular, event handlers tend to be common sources of memory leaks when they’re not maintained correctly. What do I mean by correct maintenance of event handlers?
Consider the following piece of code:
static class SystemEvents { public static event Action ShuttingDown;}class Handlers { public void OnShutDown() { // React to system shutting ...
Get C# 5.0 Unleashed now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.