Possible Duplicate:
Unsubscribe anonymous method in C# How do I Unregister 'anonymous' event handler
Ok, lets say I have the following code:
private void AttachEvent(AwesomeObject someObject)
{
int id = GetCurrentIdValue();
someUnknownClass.SomeEvent += () => someObject.CreateAwesomeness(id);
}
(essentially, I'm just trying to illustrate the need to use a local variable in an event handler as suggested in this thread). From a memory management standpoint, I can't just detach from someUnknownClass.SomeEvent because I don't have a handle on the handler. Further, let's assume someUnknownClass is a third party type, so I can't add any code within that class to detach the event internally as suggested.
My question is, how do I avoid a memory leaks while still being able to use anonymous methods as event handlers?