Skip to content

Commit

Permalink
Fix a potential memory leak in schedulePeriodically
Browse files Browse the repository at this point in the history
  • Loading branch information
zsxwing committed Feb 9, 2015
1 parent 68f7f66 commit faf9d65
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/main/java/rx/Scheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import rx.functions.Action0;
import rx.schedulers.Schedulers;
import rx.subscriptions.MultipleAssignmentSubscription;
import rx.subscriptions.SerialSubscription;

/**
* A {@code Scheduler} is an object that schedules units of work. You can find common implementations of this
Expand Down Expand Up @@ -119,11 +120,17 @@ public void call() {
if (!mas.isUnsubscribed()) {
action.call();
long nextTick = startInNanos + (++count * periodInNanos);
mas.set(schedule(this, nextTick - TimeUnit.MILLISECONDS.toNanos(now()), TimeUnit.NANOSECONDS));
SerialSubscription s = new SerialSubscription();
// Should call `mas.set` before `schedule`, or the new Subscription may replace the old one.
mas.set(s);
s.set(schedule(this, nextTick - TimeUnit.MILLISECONDS.toNanos(now()), TimeUnit.NANOSECONDS));
}
}
};
mas.set(schedule(recursiveAction, initialDelay, unit));
SerialSubscription s = new SerialSubscription();
// Should call `mas.set` before `schedule`, or the new Subscription may replace the old one.
mas.set(s);
s.set(schedule(recursiveAction, initialDelay, unit));
return mas;
}

Expand Down

1 comment on commit faf9d65

@rengwuxian
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How could the memory be leaked before?

Please sign in to comment.