{"action":"created","issue":{"url":"https://api.github.com/repos/dotnet/roslyn/issues/101","labels_url":"https://api.github.com/repos/dotnet/roslyn/issues/101/labels{/name}","comments_url":"https://api.github.com/repos/dotnet/roslyn/issues/101/comments","events_url":"https://api.github.com/repos/dotnet/roslyn/issues/101/events","html_url":"https://github.com/dotnet/roslyn/issues/101","id":55722791,"number":101,"title":"Provide simple syntax to create a weak-referenced eventhandler","user":{"login":"dotMorten","id":1378165,"avatar_url":"https://avatars.githubusercontent.com/u/1378165?v=3","gravatar_id":"","url":"https://api.github.com/users/dotMorten","html_url":"https://github.com/dotMorten","followers_url":"https://api.github.com/users/dotMorten/followers","following_url":"https://api.github.com/users/dotMorten/following{/other_user}","gists_url":"https://api.github.com/users/dotMorten/gists{/gist_id}","starred_url":"https://api.github.com/users/dotMorten/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dotMorten/subscriptions","organizations_url":"https://api.github.com/users/dotMorten/orgs","repos_url":"https://api.github.com/users/dotMorten/repos","events_url":"https://api.github.com/users/dotMorten/events{/privacy}","received_events_url":"https://api.github.com/users/dotMorten/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/dotnet/roslyn/labels/Feature%20Request","name":"Feature Request","color":"fc2929"},{"url":"https://api.github.com/repos/dotnet/roslyn/labels/Language-C%23","name":"Language-C#","color":"207de5"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2015-01-28T07:20:52Z","updated_at":"2015-01-28T21:25:15Z","closed_at":null,"body":"The by far most common memory leak people introduce in .NET apps are caused by event handlers added to longer-lived objects. For instance an event on a static instance, or on an instance who's lifetime is the entire app lifetime (like app view model or app settings).\r\nListening for events on this from a shorter-lived object, makes this short-lived object hang around in memory until the app stops running. A common scenario is the INotifyCollectionChanged event listened to by a XAML control, with data coming from a collection provided by some app state. This will cause a very memory-consuming object like a UIElement to stay around in memory until the app closes. The recommended pattern to avoid this is by using WeakReference preferable wrapped in an event manager / event proxy class. However this can be quite a lot of code to write, and the implementation is not straight forward, and will still cause the small proxy hang around at least until the event fires again (if it ever does).\r\n\r\nIt would be much simpler if a syntax alternative to \"+=\" would denote \"weak\". For example:\r\n```\r\n    staticInstance.Data.CollectionChanged += MyChangedHandler; //not weak event reference\r\n    staticInstance.Data.CollectionChanged +~ MyChangedHandler; //weak event reference \r\n```\r\nI'm not saying '+~' should actually be the syntax - just that we have something as simple as this to solve a very common scenario.\r\n\r\nUservoice suggestion for voting: http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/7019993-provide-simple-syntax-to-create-a-weak-referenced\r\n"},"comment":{"url":"https://api.github.com/repos/dotnet/roslyn/issues/comments/71919220","html_url":"https://github.com/dotnet/roslyn/issues/101#issuecomment-71919220","issue_url":"https://api.github.com/repos/dotnet/roslyn/issues/101","id":71919220,"user":{"login":"matwilko","id":3772498,"avatar_url":"https://avatars.githubusercontent.com/u/3772498?v=3","gravatar_id":"","url":"https://api.github.com/users/matwilko","html_url":"https://github.com/matwilko","followers_url":"https://api.github.com/users/matwilko/followers","following_url":"https://api.github.com/users/matwilko/following{/other_user}","gists_url":"https://api.github.com/users/matwilko/gists{/gist_id}","starred_url":"https://api.github.com/users/matwilko/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/matwilko/subscriptions","organizations_url":"https://api.github.com/users/matwilko/orgs","repos_url":"https://api.github.com/users/matwilko/repos","events_url":"https://api.github.com/users/matwilko/events{/privacy}","received_events_url":"https://api.github.com/users/matwilko/received_events","type":"User","site_admin":false},"created_at":"2015-01-28T21:25:14Z","updated_at":"2015-01-28T21:25:14Z","body":"@dotMorten That may not hold up based on what optimizations the compiler might quite correctly perform.\r\n\r\nIf you don't use the handler local again after those initial two lines, the compiler has every reason to consider it \"dead\" because you never access it again, and so a valid optimization might be to simply reuse that local (in CIL terms) again if you define another EventHandler local further down the method.\r\n\r\nBecause of this, your assertion that you're \"pinning\" the handler in the local isn't true under all conditions, and it can still become immediately eligible for garbage collection."}}