@@ -114,9 +114,7 @@ public void Dispose()
114
114
_scheduledExpiration ? . Cancellation . Dispose ( ) ;
115
115
}
116
116
117
- private IDisposable OnScheduledExpirationInvoked (
118
- IScheduler scheduler ,
119
- Expiration intendedExpiration )
117
+ private IDisposable OnScheduledExpirationInvoked ( Expiration intendedExpiration )
120
118
{
121
119
try
122
120
{
@@ -256,9 +254,20 @@ private void FinishSchedulingExpiration(
256
254
ScheduledExpiration unfinishedExpiration ,
257
255
IScheduler scheduler )
258
256
=> unfinishedExpiration . Cancellation . Disposable = scheduler . Schedule (
259
- state : unfinishedExpiration . Expiration ,
257
+ state : (
258
+ thisReference : new WeakReference < Subscription > ( this ) ,
259
+ expiration : unfinishedExpiration . Expiration ) ,
260
260
dueTime : unfinishedExpiration . Expiration . ExpireAt ,
261
- action : OnScheduledExpirationInvoked ) ;
261
+ action : static ( _ , state ) =>
262
+ {
263
+ // Most schedulers won't clear scheduled actions upon cancellation, they'll wait until they were supposed to occur.
264
+ // A WeakReference here prevents the whole subscription from memory leaking
265
+ // Refer to https://github.com/reactivemarbles/DynamicData/issues/1025
266
+ if ( state . thisReference . TryGetTarget ( out var @this ) )
267
+ @this . OnScheduledExpirationInvoked ( state . expiration ) ;
268
+
269
+ return Disposable . Empty ;
270
+ } ) ;
262
271
263
272
private void TryPublishCompletion ( )
264
273
{
0 commit comments