Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I be calling Dispose on Reactive Extensions (Rx) Subject<T>

I am using the Reactive Extensions (Rx) Subject as a direct replacement for C# events like so:

public class MyClass
{
    private Subject<string> subject;

    public IObservable<string> WhenSomethingHappened
    {
        get { return this.subject.AsObservable(); }
    }

    private void OnSomethingHappened(string something)
    {
        this.subject.OnNext(something);
    }
}

Note that I never call OnCompleted on my subject. Should MyClass be implementing IDisposable and calling this.subject.Dispose? This would mean that any implementation using Subject should implement IDisposable.

The reason I ask is that the IDisposable pattern is a bit like a disease, if one thing implements it, everything that uses it has to implement it too.

like image 254
Muhammad Rehan Saeed Avatar asked Jun 20 '14 08:06

Muhammad Rehan Saeed


1 Answers

Nope, you don't really need to do this. When worrying about memory usage and lifetimes, think about disposing Subscriptions, not Subjects.

like image 65
Ana Betts Avatar answered Oct 24 '22 04:10

Ana Betts