diff --git a/src/ReactiveUI.Tests/ObservableAsPropertyHelper/ObservableAsPropertyHelperTest.cs b/src/ReactiveUI.Tests/ObservableAsPropertyHelper/ObservableAsPropertyHelperTest.cs index 205aa59951..d5074c94d7 100644 --- a/src/ReactiveUI.Tests/ObservableAsPropertyHelper/ObservableAsPropertyHelperTest.cs +++ b/src/ReactiveUI.Tests/ObservableAsPropertyHelper/ObservableAsPropertyHelperTest.cs @@ -162,6 +162,25 @@ public void OAPHDeferSubscriptionParameterIsSubscribedIsNotTrueInitially() Assert.True(fixture.IsSubscribed); } + [Fact] + public void OAPHDeferSubscriptionShouldNotThrowIfDisposed() + { + var observable = Observable.Create(o => + { + o.OnNext(42); + o.OnCompleted(); + + return Disposable.Empty; + }); + + var fixture = new ObservableAsPropertyHelper(observable, _ => { }, 0, true); + + Assert.False(fixture.IsSubscribed); + fixture.Dispose(); + var ex = Record.Exception(() => Assert.Equal(0, fixture.Value)); + Assert.Null(ex); + } + [Fact] public void OAPHShouldRethrowErrors() { diff --git a/src/ReactiveUI/ObservableForProperty/ObservableAsPropertyHelper.cs b/src/ReactiveUI/ObservableForProperty/ObservableAsPropertyHelper.cs index 277dadb3cd..cbf0c4515b 100644 --- a/src/ReactiveUI/ObservableForProperty/ObservableAsPropertyHelper.cs +++ b/src/ReactiveUI/ObservableForProperty/ObservableAsPropertyHelper.cs @@ -48,7 +48,7 @@ public sealed class ObservableAsPropertyHelper : IHandleObservableErrors, IDi /// A value indicating whether the /// should defer the subscription to the source /// until the first call to , or if it should immediately - /// subscribe to the the source. + /// subscribe to the source. /// /// /// The scheduler that the notifications will be provided on - @@ -85,7 +85,7 @@ public ObservableAsPropertyHelper( /// A value indicating whether the /// should defer the subscription to the source /// until the first call to , or if it should immediately - /// subscribe to the the source. + /// subscribe to the source. /// /// /// The scheduler that the notifications will provided on - this @@ -136,7 +136,12 @@ public T Value { if (Interlocked.CompareExchange(ref _activated, 1, 0) == 0) { - _source.Subscribe(_subject).DisposeWith(_disposable); + // Do not subscribe if disposed + var localReferenceInCaseDisposeIsCalled = _disposable; + if (localReferenceInCaseDisposeIsCalled != null) + { + _source.Subscribe(_subject).DisposeWith(localReferenceInCaseDisposeIsCalled); + } } return _lastValue;