-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide simple syntax to create a weak-referenced eventhandler #101
Comments
What exactly would |
@svick You can completely avoid the memory leak by using a |
@sharwell Thank you for the ConditionalWeakTable option. However it doesn't change the fact that this is a common problem that there should be a simple syntax for accomplishing. |
@dotMorten We are on the same page regarding the challenges of this functionality, which is essential for many kinds of applications. A nuance of the static void SomeUtilityMethod(DiagnosticLog log)
{
AppDomain.AssemblyLoad +~ (sender, e) => log.WriteLine("Assembly Loaded");
} How would you support attaching the weak event to Other ApproachesThe closest syntax you can have right now is the following: environmentData.CollectionChanged += AsWeak<TEventArgs>(this, HandleCollectionChanged); Where static EventHandler<TEventArgs> AsWeak<TEventArgs>(object owner, EventHandler<TEventArgs> handler); It would be safer to have a generic owner in order to use a generic type constraint and avoid boxing: static EventHandler<TEventArgs> AsWeak<T, TEventArgs>(T owner, EventHandler<TEventArgs> handler)
where T : class However, this would increase the verbosity of usage: environmentData.CollectionChanged += AsWeak<ThisType, TEventArgs>(this, HandleCollectionChanged); Aside from your syntax, the use of weak event handlers would be simplified by improving the ability of the compiler to resolve the generic arguments for a call of this form. |
@sharwell Wrt to your example static void SomeUtilityMethod(DiagnosticLog log)
{
AppDomain.AssemblyLoad +~ (sender, e) => log.WriteLine("Assembly Loaded");
} You are declaring an anonymous delegate here, which goes out of scope immediately. This means it's up for garbage collection right away. In this scenario you would never want to do this anonymously. I can see how this might be confusing, but IMHO is very much expected and wanted behavior. Instead this would make more sense: static void SomeUtilityMethod(DiagnosticLog log)
{
EventHandler handler = (sender, e) => log.WriteLine("Assembly Loaded"); //pin handler to local scope
AppDomain.AssemblyLoad +~ handler;
//perform some assembly loading
//
//No need to remember to do this because of weak event: AppDomain.AssemblyLoad -= handler;
} //'handler' out of scope and unhooks from event. |
@dotMorten That may not hold up based on what optimizations the compiler might quite correctly perform. If 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. Because 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. |
@dotMorten In the example I gave, the life of the |
This is one of my top wishes, because memory leak through event handlers are very common. And the other way around? I declare an event as weak, not the subscribers, so the subscribers can only listen as long as the event is alive and fired?
|
@JohnnyBravo75 How is that different from normal events? An event is basically just a list of delegates. If the object that holds the list gets GCed, so will the delegates (and potentially also the subscribers). |
@svick it's not different but the reference back often surprises people and causes stuff to "leak". And example is listening to events on static instances. Your class listening will stay in memory for the entire duration of the app. Quite often this isn't intended. Normally when you hold a reference to a static object its fine - your object can still be GC'ed. But because the event handler "points the other direction" this means the longer lived object unintentionally holds on to the shorter lived. |
@dotMorten Yes, I understand that and I think that some way to fix that should be in the language. My question was specifically aimed at @JohnnyBravo75's comment about his version of weak event where "the subscribers can only listen as long as the event is alive". Because that's how normal events already work, subscribers don't keep the object with event alive. |
An ideal way to manage this kind of thing would be to make WeakReference cheaper and easier to utilize. Effectively a way to keep a handle on an object until that object is no longer valid without the curse of keeping the object valid long beyond its intended use simply because you have a handle on it. This is a bit of a crusade of mine because there's is no explicit way to free memory in C# and nearly all memory leaks are caused by people not realizing that they're keeping memory locked up long after they no longer need it (event handlers being a huge source of problems currently). A weak reference should not impact GC logic, but should be set to null when the GC cleans up the memory associated with the allocation.
If the community feels that call site markup is necessary to help explain / make more explicit / etc the assignment of weak references, then the
|
I'm confused, in what way doesn't
Again, isn't that exactly what |
@svick yes it is what Today, we have a cycle of "it's ugly, scary, complicated, and/or non-performant so I don't use them", followed by "nobody uses them, so it's low priority" followed by the initial statement. |
@svick Suggested: Alternative suggestion: When an event is declared as weak, all reference are automatic converted to weak references, the event holds only a list of weak delegate references. Its just the matter, does the subscriber decide, or does the event decide that a reference is weak and this I wanted to suggest to think about. |
I like your approach, because it is much more general and not limited to events, as I understand? Btw: Big frameworks/programs often (also xaml/the bindingengine) make heavy use of weak references, without them they would leak. |
@JohnnyBravo75 given that events are subscription base, I suggest that they always use weak references. In fact, at the moment, I'm having trouble coming up with a reason they should not. |
Events should definitely not default to weak. As pointed out higher up that would break a lot of existing code - the more obvious one anonymous delegates:
Ii think cases like this should result in a compiler error. Using weak events on handlers declared in local scope should not be allowed. Only event handlers that are member types of instances should be allowed (I don't even think static methods as handlers should be allowed, since that method would never be released, but I guess there's no harm in allowing it either). @JohnnyBravo75 I don't see the point of declaring events as weak - that would mean an API would impose weak listeners on its users. And it wouldn't allow me to use weak listeners on existing events, like PropertyChanged and CollectionChanged events which are the most common culprits in XAML for this problem. @whoisj Making all events weak by default would be a HUGE breaking change, and not an option. |
@dotMorten ahh and there's the example that I wasn't coming up with. Thanks for that. Anonymous delegates aside, having event references be weak by default brings a lot of benefits. Perhaps the compiler could smart and helpful enough to track anonymous delegates as strong references - though that seems like a lot of book keeping for the GC to be doing. Still, I stand by my assertion that weak references need to be first class citizens, used more often, and have a language feature to encourage their usage. |
If you don't want to wait for new syntax, here's a helper class that allows you to create weak delegates from my own class library (which I did not put on GitHub so far put plan to do 'if I have time' ;-) |
@whiletrue-eu The implementation you have is pretty standard to solve this, but also quite syntax heavy to use. The outcome is people don't do it, when they need to - especially when dealing with XAML where you might be hanging on to big UI Pages due to this. Hence this suggestion. Also the implementation has the downside of actually still leaking - it just leaks a much smaller object. If the event fires after the reference has been released, it will release this small bit of memory, but if the event never fires it'll never get cleaned up completely. |
Hi Morten, Morten Nielsen [email protected] schrieb:
|
What's about WeakDelegate class, that uses WeakReference instead? And just does nothing, if target is not alive anymore. Currently: Imagine that: No CLR changes, small compiler changes, just a new Delegate-derived class and small syntax sugar. |
I created something to resolve this some time ago, the code can been seen at https://github.com/MundusSoft/Mundus.Toolkit/blob/master/Mundus.Toolkit/WeakEventManager.cs. |
@AlexPalma72 Thanks. As mentioned above it is already possible and other people have similar solutions. That's not really what this is about. The solutions are all somewhat code-heavy. What this issue is trying to achieve is a simple syntax that automatically makes events weak, and makes it so easy people just do it and become used to it. |
I think someObject.Event += weak (sender, e) => {};
// translate to something like
System.Runtime.WeakEvent.Attach(someObject, nameof(TSomeObject.Event), (sender, e) => {}); Same var obj = weak new List<string>();
// translate to
var obj = new WeakReference<List<string>>(new List<string>());
public class AClass
{
private weak List<string> weakList;
}
// translate to
public class AClass
{
[WeakReference]
private WeakReference<List<String>> weakList;
} As List<string> hardList;
weak ListString<string> weakList = hardList;
// translate to
if (hardList == null)
{
weakList = null;
} else
{
weakList = new WeakReference<List<string>>(hardList);
} Usage of declared weak references in code // declare weak reference
weak List<string> weakList = new List<string>();
// same as
var weakList = weak new ListString();
// first declaration example is explicit variable type declaration, second - using "var" implicit typing
var cnt = weakList.Count;
// translate to:
int cnt;
{
List<string> __weak_t;
if (weakList != null && weakList.TryGetTarget(out __weak_t))
{
cnt = __weak_t.Count;
} else
{
throw new InvalidWeakReferenceException();
}
}
List<string> hardList = weakList;
// translate to
List<string> hardList;
if (weakList == null || !weakList.TryGetTarget(out hardList))
{
throw new InvalidWeakReferenceException();
}
// use ?. operator to not throw exception if weak reference is invalid
var cnt = weakList?.Count;
// translate to
int? cnt;
{
List<string> __weak_t;
if (weakList != null && weakList.TryGetTarget(out __weak_t))
{
cnt = __weak_t.Count;
} else
{
cnt = null;
}
}
// adding ? at the end to not throw exception
List<string> hardList = weakList?;
// translate to
List<string> hardList;
if (weakList == null || !weakList.TryGetTarget(out hardList))
{
hardList = null
}
// Checking if weak reference is alive by using ? operator
if (weakList? != null)
{
Console.WriteLine("Weak reference is alive!");
}
// translate to
{
List<string> __weak_t;
if (weakList == null || !weakList.TryGetTarget(out __weak_t))
{
__weak_t = null;
}
if (__weak_t != null)
{
Console.WriteLine("Weak reference is alive!");
}
}
// Call a method
weakList.Clear();
// translate to
{
List<string> __weak_t;
if (weakList == null || !weakList.TryGetTarget(out __weak_t))
{
throw new InvalidWeakReferenceException();
}
__weak_t.Clear();
}
// Call a method not throwing an exception if reference is invalid
weakList?.Clear();
// translate to
{
List<string> __weak_t;
if (weakList != null && weakList.TryGetTarget(out __weak_t))
{
__weak_t.Clear();
}
} And types var t = typeof(weak List<string>);
// same as
var t = typeof(WeakReference<List<string>>);
weakList.GetType() // result: typeof(WeakReference<List<string>>)
weakList?.GetType() // result: typeof(List<string>) |
The LDM discussed this. Most of the solution needs to be in a library. And given such a solution, language support adds little value. We do not intend to move forward with this as a championed proposal for a C# language change. |
@gafter Could you please elaborate on what was discussed? I don't understand why this needs to be in a library. We can already write it in code, and its tedious ugly code - even after using helper-methods in a library, and that still creates tiny memory leaks. The entire purpose of the language proposal was to help people consider avoiding these types of memory leaks by making a much much easier no-brainer way to do this. |
@dotMorten There is little that the compiler can do that would avoid those tiny memory leaks. @mattwar Can you expand on this, perhaps pointing to the blog posts? |
The memory leaks are a implementation problem, but I do not believe that's what @dotMorten is concerned with. As I understand it, he's looking for syntactic sugar that makes use of weak references cleaner to write/read. |
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).
Listening 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).
It would be much simpler if a syntax alternative to "+=" would denote "weak". For example:
I'm not saying '+~' should actually be the syntax - just that we have something as simple as this to solve a very common scenario.
Uservoice suggestion for voting: http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/7019993-provide-simple-syntax-to-create-a-weak-referenced
The text was updated successfully, but these errors were encountered: