Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
190 views
in Technique[技术] by (71.8m points)

c# - Do I have to worry about memory leaks with Rx.NET FromEventPattern?

In .NET I've been essentially raised to never ever forget to unsubscribe to events. In MVVM apps what I often end up with is this construct.

public class WindowVm
{
    public EntityModel MyModel { get; set; }

    void Subscribe()
    {
        MyModel.PropertyChanged += DoSomething;
    }

    void Unsubscribe()
    {
        MyModel.PropertyChanged -= DoSomething;
    }
}

I need to unsubscribe, because if I don't MyModel would keep a reference to the WindowVm and keep it alive for as long as MyModel lives.

I just found out about reactive extensions, but I can't find out whether I need to still think about this. I cant find anywhere that says so, but I also don't hear "oh and btw it solves that annoying event unsubscribe problem" which would be a killer argument.

public class WindowVm
{
    public EntityModel MyModel { get; set; }

    void Subscribe()
    {
        MyModel.PropChangedObservable.Subscribe(e => DoSomething(e.Sender, e.EventArgs));
    }
}
public class EntityModel : ObservableBase
{
    public IObservable<EventPattern<PropertyChangedEventArgs>> PropChangedObservable { get; private set; }

    public EntityModel()
    {
        PropChangedObservable = Observable
               .FromEventPattern<PropertyChangedEventHandler, PropertyChangedEventArgs>(
                   x => this.PropertyChanged += x,
                   x => this.PropertyChanged -= x);
    }
}

I can't tell whether this would create an extra reference. And what about this way?

public class WindowVm
{
    public EntityModel MyModel { get; set; }

    void Subscribe()
    {
        Observable
               .FromEventPattern<PropertyChangedEventHandler, PropertyChangedEventArgs>(
                   x => MyModel.PropertyChanged += x,
                   x => MyModel.PropertyChanged -= x)
               .Subscribe(e => DoSomething(e.Sender, e.EventArgs));
    }
}
question from:https://stackoverflow.com/questions/65863688/do-i-have-to-worry-about-memory-leaks-with-rx-net-fromeventpattern

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I have not worked with RX but the Subscribe method that you are using is returning an IDisposable which should then be used by the consumer to unsubscribe. Your method:

void Subscribe()
{    
    MyModel.PropChangedObservable.Subscribe(e => DoSomething(e.Sender, e.EventArgs));
}

Should then be:

IDisposable Subscribe()
{    
    return MyModel.PropChangedObservable.Subscribe(e => DoSomething(e.Sender, e.EventArgs));
}

Note: I'm guessing your code is not 100% complete as you are missing the method to be invoked on Subscribe and you have hard-coded DoSomething and I'm ignoring that part

So the observer of your model would first call subscribe and acquire the reference to IDisposable. Once the observer is finished then it should call Dispose on that reference.

To answer your question

I can't find out whether I need to still think about this

The answer is yes, you still need to think about this. However the RX does have automatic unsubscription but you need to know how many events you want to listen to. Check the answer of this SO question.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...